package cn.iocoder.yudao.module.mes.controller.admin.producereport;

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.mes.controller.admin.producereport.vo.ProduceReportDetailPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailSaveReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ReportTypeEnum;
import cn.iocoder.yudao.module.mes.dal.dataobject.producereport.ProduceReportDetailDO;
import cn.iocoder.yudao.module.mes.service.producereport.ProduceReportDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

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

@Tag(name = "管理后台 - 生产报工明细")
@RestController
@RequestMapping("/mes/produce-report-detail")
@Validated
public class ProduceReportDetailController {

    @Resource
    private ProduceReportDetailService produceReportDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建生产报工明细")
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:create')")
    public CommonResult<Long> createProduceReportDetail(@Valid @RequestBody ProduceReportDetailSaveReqVO createReqVO) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(createReqVO.getReportDateString(), formatter);
        createReqVO.setReportDate(localDateTime);
        return success(produceReportDetailService.createProduceReportDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产报工明细")
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:update')")
    public CommonResult<Boolean> updateProduceReportDetail(@Valid @RequestBody ProduceReportDetailSaveReqVO updateReqVO) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(updateReqVO.getReportDateString(), formatter);
        updateReqVO.setReportDate(localDateTime);
        produceReportDetailService.updateProduceReportDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产报工明细")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:delete')")
    public CommonResult<Boolean> deleteProduceReportDetail(@RequestParam("id") Long id) {
        produceReportDetailService.deleteProduceReportDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产报工明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:query')")
    public CommonResult<ProduceReportDetailRespVO> getProduceReportDetail(@RequestParam("id") Long id) {
        ProduceReportDetailDO produceReportDetail = produceReportDetailService.getProduceReportDetail(id);
        return success(BeanUtils.toBean(produceReportDetail, ProduceReportDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产报工明细分页")
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:query')")
    public CommonResult<PageResult<ProduceReportDetailRespVO>> getProduceReportDetailPage(@Valid ProduceReportDetailPageReqVO pageReqVO) {
        PageResult<ProduceReportDetailRespVO> pageResult = produceReportDetailService.getProduceReportDetailPage(pageReqVO);
        return success(pageResult);
    }
    @GetMapping("/pageCenter")
    @Operation(summary = "获得生产报工明细分页")
    public CommonResult<PageResult<ProduceReportDetailRespVO>> getProduceReportDetailPageCenter(@Valid ProduceReportDetailPageReqVO pageReqVO) {
        if(pageReqVO.getReportType().equals(ReportTypeEnum.个人.getValue())){
            pageReqVO.setReportType(null);
            Long userId =  getLoginUserId();
            pageReqVO.setUserId(userId);
        }
        PageResult<ProduceReportDetailRespVO> pageResult = produceReportDetailService.getProduceReportDetailPage(pageReqVO);
        return success(pageResult);
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出生产报工明细 Excel")
    //@PreAuthorize("@ss.hasPermission('mes:produce-report-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProduceReportDetailExcel(@Valid ProduceReportDetailPageReqVO pageReqVO,
                                               HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProduceReportDetailRespVO> list = produceReportDetailService.getProduceReportDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产计件报表.xls", "数据", ProduceReportDetailRespVO.class,
                list);
    }

    @GetMapping("/updateStatus")
    @Operation(summary = "提交报工单")
    public CommonResult<Boolean> updateStatus(@RequestParam("id") Long id,
                                                                @RequestParam("status") Integer status) {
        produceReportDetailService.updateStatus(id,status);
        return success(true);
    }
}