package cn.ezhizao.project.business.technologicalProcessDetail.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.supplier.domain.BizSupplierProductProcess;
import cn.ezhizao.project.business.supplier.service.IBizSupplierProductProcessService;
import cn.ezhizao.project.business.technologicalProcess.domain.BizTechnologicalProcess;
import cn.ezhizao.project.business.technologicalProcessDetail.domain.BizTechnologicalProcessDetail;
import cn.ezhizao.project.business.technologicalProcessDetail.service.IBizTechnologicalProcessDetailService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 工艺工序中间表Controller
 *
 * @author ezhizao
 * date    2023-11-16
 */
@RestController
@RequestMapping("/base/technologicalProcessDetail")
public class BizTechnologicalProcessDetailController extends BaseController
{
    @Resource
    private IBizTechnologicalProcessDetailService bizTechnologicalProcessDetailService;
    @Resource
    private IBizSupplierProductProcessService bizSupplierProductProcessService;
    /**
     * 查询工艺工序中间表列表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:list')")
    @Log(title = "标准工艺下的工序查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizTechnologicalProcessDetail bizTechnologicalProcessDetail) throws NoSuchFieldException, IllegalAccessException
    {
        List<BizTechnologicalProcessDetail> list = bizTechnologicalProcessDetailService.getList(bizTechnologicalProcessDetail);
        List<BizTechnologicalProcessDetail> item1 = new ArrayList<>();
        List<BizTechnologicalProcessDetail> item2 = new ArrayList<>();
        for(BizTechnologicalProcessDetail i : list){
            if (i.getTenantId() !=9){
                item1.add(i);
            }else {
                item2.add(i);
            }
        }
    item1.addAll(item2);
        return getDataTable(item1);
    }
    @Log(title = "标准工艺id查询工序", businessType = BusinessType.SELECT)
    @GetMapping("/getTechnologicalProcessDetails")
    public AjaxResult getTechnologicalProcessDetails(BizTechnologicalProcessDetail bizTechnologicalProcess) {
        List<BizTechnologicalProcessDetail> list = bizTechnologicalProcessDetailService.getTechnologicalProcessDetails(bizTechnologicalProcess);
        return success(list);
    }
    @Log(title = "外协商选择工序查询", businessType = BusinessType.SELECT)
    @PostMapping("/getProcessesForSupplier")
    public AjaxResult getProcessesForSupplier(@RequestBody Map<String, Object> data) {
        Long productId = Long.parseLong(data.get("productId").toString());
        Long supplierId = Long.parseLong(data.get("supplierId").toString());
        String technologyVersion = data.get("technologyVersion").toString();
        String processAlias = data.get("processAlias").toString();
        //查询已经添加进来的，则不显示
        List<BizSupplierProductProcess> processList = bizSupplierProductProcessService.query().eq("product_id", productId).eq("supplier_id", supplierId).eq("technology_version", technologyVersion).list();

        List<BizTechnologicalProcessDetail> list = bizTechnologicalProcessDetailService.query()
                .eq("product_id", productId)
                .eq("technology_version", technologyVersion)
                .like("process_alias", processAlias)
                .list();
        if(!processList.isEmpty()){
            List<String> processStepNumberList = processList.stream().map(BizSupplierProductProcess::getProcessStepNumber).collect(Collectors.toList());
            list = list.stream().filter(v-> !processStepNumberList.contains(v.getProcessStepNumber())).collect(Collectors.toList());
        }

        return success(list);
    }

    /**
     * 导出工艺工序中间表列表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:export')")
    @Log(title = "工艺工序导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizTechnologicalProcessDetail bizTechnologicalProcessDetail) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizTechnologicalProcessDetail);
        List<BizTechnologicalProcessDetail> list = bizTechnologicalProcessDetailService.getList(bizTechnologicalProcessDetail);
        ExcelUtil<BizTechnologicalProcessDetail> util = new ExcelUtil<BizTechnologicalProcessDetail>(BizTechnologicalProcessDetail.class);
        util.exportExcel(response, list, "工艺工序中间表数据");
    }

    /**
     * 获取工艺工序中间表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:product:query')")
    @Log(title = "标准工艺下的工序查询详细信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizTechnologicalProcessDetailService.getById(id));
    }

    /**
     * 新增工艺工序中间表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:add')")
    @Log(title = "标准工艺下工序保存", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody List<BizTechnologicalProcessDetail> bizTechnologicalProcessDetail)
    {
//        String tenantId = request.getHeader("tenantId");
//        for (BizTechnologicalProcessDetail item : bizTechnologicalProcessDetail) {
//            item.setTenantId(Long.valueOf(tenantId));
//        }
        List<BizTechnologicalProcessDetail> oldList = bizTechnologicalProcessDetailService.query().eq("technological_process_id", bizTechnologicalProcessDetail.get(0).getTechnologicalProcessId()).list();
        boolean batch = saveOrUpdateBatch(bizTechnologicalProcessDetailService, bizTechnologicalProcessDetail, oldList);
        return toAjax(batch);
    }

    /**
     * 新增工艺工序中间表单条操作
     */
//    @PreAuthorize("@ss.hasPermi('business:product:add')")
    @Log(title = "标准工艺下工序单条保存", businessType = BusinessType.INSERT)
    @PostMapping("/singleSave")
    public AjaxResult singleSave(@RequestBody BizTechnologicalProcessDetail bizTechnologicalProcessDetail)
    {
//        String tenantId = request.getHeader("tenantId");
//        bizTechnologicalProcessDetail.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizTechnologicalProcessDetailService.saveOrUpdate(bizTechnologicalProcessDetail));
    }

    /**
     * 修改工艺工序中间表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:edit')")
    @Log(title = "标准工艺下工序修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTechnologicalProcessDetail bizTechnologicalProcessDetail) throws NoSuchFieldException, IllegalAccessException
    {
//        String tenantId = request.getHeader("tenantId");
//        bizTechnologicalProcessDetail.setTenantId(Long.valueOf(tenantId));
        return toAjax(bizTechnologicalProcessDetailService.updateById(bizTechnologicalProcessDetail));
    }

    /**
     * 删除工艺工序中间表
     */
//    @PreAuthorize("@ss.hasPermi('business:product:remove')")
    @Log(title = "标准工艺下工序删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {

        return toAjax(bizTechnologicalProcessDetailService.removeBatchByIds(ids));
    }
}
