package com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingPageReqVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingRespVO;
import com.zt.plat.module.contractorder.controller.admin.originalmaterialprocessing.vo.OriginalMaterialProcessingSaveReqVO;

import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.DownOrUpOrderReqVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.DownOrUpOrderRespVO;
import com.zt.plat.module.contractorder.controller.admin.purchaseorder.vo.LinkOrderReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.originalmaterialprocessing.OriginalMaterialProcessingDO;
import com.zt.plat.module.contractorder.service.originalmaterialprocessing.OriginalMaterialProcessingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 来料加工")
@RestController
@RequestMapping("/base/original-material-processing")
@Validated
public class OriginalMaterialProcessingController {

    @Resource
    private OriginalMaterialProcessingService originalMaterialProcessingService;

    @PostMapping("/create")
    @Operation(summary = "创建来料加工订单")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:create')")
    public CommonResult<OriginalMaterialProcessingRespVO> createOriginalMaterialProcessingOrder(@Valid @RequestBody OriginalMaterialProcessingSaveReqVO createReqVO) {
        return success(originalMaterialProcessingService.createOriginalMaterialProcessing(createReqVO));
    }

    @PostMapping("/create-batch")
    @Operation(summary = "批量创建来料加工订单")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:create')")
    public CommonResult<List<OriginalMaterialProcessingRespVO>> createBatchOriginalMaterialProcessingOrder(@Valid @RequestBody List<OriginalMaterialProcessingSaveReqVO> createReqVOS) {
        return success(originalMaterialProcessingService.createBatchOriginalMaterialProcessing(createReqVOS));
    }

    @PutMapping("/update")
    @Operation(summary = "更新来料加工订单")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:update')")
    public CommonResult<Boolean> updateOriginalMaterialProcessingOrder(@Valid @RequestBody OriginalMaterialProcessingSaveReqVO updateReqVO) {
        originalMaterialProcessingService.updateOriginalMaterialProcessing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除来料加工订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:delete')")
    public CommonResult<Boolean> deleteOriginalMaterialProcessingOrder(@RequestParam("id") Long id) {
        originalMaterialProcessingService.deleteOriginalMaterialProcessing(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除来料加工订单")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:delete')")
    public CommonResult<Boolean> deleteOriginalMaterialProcessingOrderList(@RequestBody BatchDeleteReqVO req) {
        originalMaterialProcessingService.deleteOriginalMaterialProcessingListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得来料加工订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:query')")
    public CommonResult<OriginalMaterialProcessingRespVO> getOriginalMaterialProcessingOrder(@RequestParam("id") Long id) {
        OriginalMaterialProcessingDO originalMaterialProcessingDO = originalMaterialProcessingService.getOriginalMaterialProcessing(id);
        OriginalMaterialProcessingRespVO bean = BeanUtils.toBean(originalMaterialProcessingDO, OriginalMaterialProcessingRespVO.class);
        if (bean != null) {
            bean.setDetails(originalMaterialProcessingService.getOriginalMaterialProcessingDetailListByOrderId(bean.getId()));
            if (bean.getRelatedOrderId() != null){//获取关联订单
                OriginalMaterialProcessingDO relatedOrder = originalMaterialProcessingService.getOriginalMaterialProcessing(id);
                OriginalMaterialProcessingRespVO relatedBean = BeanUtils.toBean(relatedOrder, OriginalMaterialProcessingRespVO.class);
                if (relatedBean != null){
                    relatedBean.setDetails(originalMaterialProcessingService.getOriginalMaterialProcessingDetailListByOrderId(relatedBean.getId()));
                    bean.setRelatedOrder(relatedBean);
                }
            }
        }
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得来料加工订单分页")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:query')")
    public CommonResult<PageResult<OriginalMaterialProcessingRespVO>> getOriginalMaterialProcessingOrderPage(@Valid OriginalMaterialProcessingPageReqVO pageReqVO) {
        PageResult<OriginalMaterialProcessingDO> pageResult = originalMaterialProcessingService.getOriginalMaterialProcessingPage(pageReqVO);
        PageResult<OriginalMaterialProcessingRespVO> bean = BeanUtils.toBean(pageResult, OriginalMaterialProcessingRespVO.class);
        if (bean != null && !bean.getList().isEmpty()) {
            bean.getList().forEach(item -> {
                item.setDetails(originalMaterialProcessingService.getOriginalMaterialProcessingDetailListByOrderId(item.getId()));
            });
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出来料加工订单 Excel")
    @PreAuthorize("@ss.hasPermission('base:original-material-processing-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOriginalMaterialProcessingOrderExcel(@Valid OriginalMaterialProcessingPageReqVO pageReqVO,
                                             HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OriginalMaterialProcessingDO> list = originalMaterialProcessingService.getOriginalMaterialProcessingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "来料加工订单.xls", "数据", OriginalMaterialProcessingRespVO.class,
                BeanUtils.toBean(list, OriginalMaterialProcessingRespVO.class));
    }

    //关联订单
    //关联订单
    @PostMapping("/link-order")
    @Operation(summary = "关联订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:original-material-processing-order:update','original-material-processing:order:list:OrderList:edit')")
    public CommonResult<Boolean> linkOrder(@RequestBody @Validated LinkOrderReqVO req) {
        return success(originalMaterialProcessingService.linkOrder(req));
    }

    @PostMapping("/order-by-order-id-and-type")
    @Operation(summary = "根据订单id和方式获取上或下游订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:original-material-processing-order:query')")
    public CommonResult<List<DownOrUpOrderRespVO>> getOrderByOrderIdAndType(@RequestBody DownOrUpOrderReqVO reqVO) {
        return success(originalMaterialProcessingService.getOrderByOrderIdAndType(reqVO));
    }


    @PostMapping("/bound-order")
    @Operation(summary = "获取已绑定的订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:original-material-processing-order:query')")
    public CommonResult<List<OriginalMaterialProcessingRespVO>> boundOrder(@RequestBody DownOrUpOrderReqVO reqVO) {
        return success(originalMaterialProcessingService.getBindOrderByOrder(reqVO));
    }

    //关闭订单
    @PostMapping("/close-order")
    @Operation(summary = "关闭订单")
    @PreAuthorize("@ss.hasAnyPermissions('base:original-material-processing-order:update')")
    public CommonResult<Boolean> closeOrder(@RequestBody List<Long> ids) {
        originalMaterialProcessingService.closeOrder(ids);
        return success(true);
    }
    //来料采购提交erp061
    @PostMapping("/submit-to-erp061")
    @Operation(summary = "来料采购提交erp061")
    @PreAuthorize("@ss.hasAnyPermissions('base:original-material-processing-order:update')")
    public CommonResult<Boolean> submitToErp061(@RequestParam("id") Long id) {
        originalMaterialProcessingService.submitToErp061(id);
        return success(true);
    }
}
