package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.DeviceDTO;
import com.sunyard.chsm.dto.ManufacturersDeviceDTO;
import com.sunyard.chsm.enums.ManufacturerModelEnum;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.DeviceService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 密码机管理接口
 *
 * @author liulu
 * @since 2024/10/15
 */
@Slf4j
@RestController("sp_DeviceController")
@RequestMapping("/device")
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    /**
     * 分页查询密码设备列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    @GetMapping("/pageList")
    public R<Page<DeviceDTO.DeviceView>> servicePageList(DeviceDTO.Query query) {

        Page<DeviceDTO.DeviceView> page = deviceService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 查询未分组设备列表
     *
     * @return 列表
     */
    @GetMapping("/ungrouped/list")
    public R<List<DeviceDTO.DeviceView>> getUngroupedList() {

        List<DeviceDTO.DeviceView> res = deviceService.getUngroupedList();
        return R.data(res);
    }

    /**
     * 查询设备详情
     *
     * @param id id
     * @return 详情
     */
    @GetMapping("/detail")
    public R<DeviceDTO.Detail> getById(Long id) {

        DeviceDTO.Detail res = deviceService.getDetailById(id);
        return R.data(res);
    }

    /**
     * 查询已适配的厂商设备列表
     *
     * @return 列表
     */
    @GetMapping("/adapted/list")
    public R<List<ManufacturersDeviceDTO>> getAdaptedList() {

        List<ManufacturersDeviceDTO> manufacturers = Arrays.stream(ManufacturerModelEnum.values())
                .collect(Collectors.groupingBy(ManufacturerModelEnum::getManufacturer))
                .entrySet().stream()
                .map(it -> {
                    ManufacturersDeviceDTO adaptedDevices = new ManufacturersDeviceDTO();
                    adaptedDevices.setCode(it.getKey().getCode());
                    adaptedDevices.setName(it.getKey().getName());
                    List<ManufacturersDeviceDTO.Model> modelList = it.getValue().stream().map(it2 -> {
                                ManufacturersDeviceDTO.Model model = new ManufacturersDeviceDTO.Model();
                                model.setModel(it2.getModel());
                                model.setName(it2.getName());
                                return model;
                            })
                            .collect(Collectors.toList());
                    adaptedDevices.setModels(modelList);
                    return adaptedDevices;
                })
                .collect(Collectors.toList());
        return R.data(manufacturers);
    }


    /**
     * 添加密码设备
     *
     * @param save 参数
     * @return id
     */
    @PostMapping
    @AuditControllerLog(description = "添加密码设备", operateType = AuditLogConst.ADD)
    public R<String> save(@Valid @RequestBody DeviceDTO.DeviceSave save) {
        Long id = deviceService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 修改密码设备
     *
     * @param update 参数
     * @return void
     */
    @PutMapping
    @AuditControllerLog(description = "修改密码设备", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid @RequestBody DeviceDTO.DeviceSave update) {
        deviceService.update(update);
        return R.ok();
    }

    /**
     * 删除密码设备
     *
     * @param id id
     * @return void
     */
    @DeleteMapping
    @AuditControllerLog(description = "删除密码设备", operateType = AuditLogConst.DELETE)
    public R<Void> delete(Long id) {
        deviceService.delete(id);
        return R.ok();
    }

}
