package cn.iocoder.yudao.module.pms.controller.admin.qhse.measure;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureDetectPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureDetectRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureDetectSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureBookDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureDetectDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureBookMapper;
import cn.iocoder.yudao.module.pms.service.qhse.measure.IotMeasureBookService;
import cn.iocoder.yudao.module.pms.service.qhse.measure.IotMeasureDetectService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 计量器具-检测校准明细")
@RestController
@RequestMapping("/rq/iot-measure-detect")
@Validated
public class IotMeasureDetectController {

    @Resource
    private IotMeasureDetectService iotMeasureDetectService;
    @Autowired
    private IotMeasureBookService iotMeasureBookService;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private IotMeasureBookMapper iotMeasureBookMapper;

    @PostMapping("/create")
    @Operation(summary = "创建计量器具-检测校准明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:create')")
    public CommonResult<Long> createIotMeasureDetect(@Valid @RequestBody IotMeasureDetectSaveReqVO createReqVO) {
        return success(iotMeasureDetectService.createIotMeasureDetect(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计量器具-检测校准明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:update')")
    public CommonResult<Boolean> updateIotMeasureDetect(@Valid @RequestBody IotMeasureDetectSaveReqVO updateReqVO) {
        iotMeasureDetectService.updateIotMeasureDetect(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计量器具-检测校准明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:delete')")
    public CommonResult<Boolean> deleteIotMeasureDetect(@RequestParam("id") Long id) {
        iotMeasureDetectService.deleteIotMeasureDetect(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计量器具-检测校准明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:query')")
    public CommonResult<IotMeasureDetectRespVO> getIotMeasureDetect(@RequestParam("id") Long id) {
        IotMeasureDetectDO iotMeasureDetect = iotMeasureDetectService.getIotMeasureDetect(id);
        IotMeasureDetectRespVO bean = BeanUtils.toBean(iotMeasureDetect, IotMeasureDetectRespVO.class);
        IotMeasureBookDO iotMeasureBookDO = iotMeasureBookMapper.selectById(bean.getMeasureId());
        bean.setMeasureCode(iotMeasureBookDO.getMeasureCode());
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得计量器具-检测校准明细分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:query')")
    public CommonResult<PageResult<IotMeasureDetectRespVO>> getIotMeasureDetectPage(@Valid IotMeasureDetectPageReqVO pageReqVO) {
        if (StringUtils.isNotBlank(pageReqVO.getMeasureCode())) {
            List<IotMeasureBookDO> iotMeasureBookDOS = iotMeasureBookMapper.selectList("measure_code", pageReqVO.getMeasureCode());
            if (CollUtil.isNotEmpty(iotMeasureBookDOS)) {
                pageReqVO.setMeasureId(iotMeasureBookDOS.get(0).getId());
            }
        }
        PageResult<IotMeasureDetectDO> pageResult = iotMeasureDetectService.getIotMeasureDetectPage(pageReqVO);
        List<IotMeasureDetectRespVO> collect = pageResult.getList().stream().map(e -> {
            IotMeasureDetectRespVO iotMeasureDetectRespVO = new IotMeasureDetectRespVO();
            BeanUtils.copyProperties(e, iotMeasureDetectRespVO);
            if (Objects.nonNull(iotMeasureDetectRespVO.getMeasureId())) {
                IotMeasureBookDO iotMeasureBook = iotMeasureBookService.getIotMeasureBook(iotMeasureDetectRespVO.getMeasureId());
                if (Objects.nonNull(iotMeasureBook)) {
                    iotMeasureDetectRespVO.setMeasureName(iotMeasureBook.getMeasureName());
                    iotMeasureDetectRespVO.setMeasureCode(iotMeasureBook.getMeasureCode());
                    iotMeasureDetectRespVO.setSerialNo(iotMeasureBook.getSerialNo());
                }
            }
            if (Objects.nonNull(iotMeasureDetectRespVO.getDeptId())) {
                DeptRespDTO dept = deptApi.getDept(iotMeasureDetectRespVO.getDeptId());
                if(Objects.nonNull(dept)){
                    iotMeasureDetectRespVO.setDeptName(dept.getName());
                }
            }
            return iotMeasureDetectRespVO;
        }).collect(Collectors.toList());
        return success(new PageResult<>(collect, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计量器具-检测校准明细 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-measure-detect:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMeasureDetectExcel(@Valid IotMeasureDetectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMeasureDetectDO> list = iotMeasureDetectService.getIotMeasureDetectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计量器具-检测校准明细.xls", "数据", IotMeasureDetectRespVO.class,
                        BeanUtils.toBean(list, IotMeasureDetectRespVO.class));
    }

}