package cn.ezhizao.project.business.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.project.business.domain.BizCarrier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.project.business.domain.BizOutsourcedOrderDetailProcess;
import cn.ezhizao.project.business.service.IBizOutsourcedOrderDetailProcessService;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.web.page.TableDataInfo;

/**
 * 外协单从对应的外协工序的从Controller
 *
 * @author 庄德政
 * date    2024-04-26
 */
@RestController
@RequestMapping("/business/outsourceDetailProcess")
public class BizOutsourcedOrderDetailProcessController extends BaseController
{
    @Resource
    private IBizOutsourcedOrderDetailProcessService bizOutsourcedOrderDetailProcessService;
    @Resource
    private HttpServletRequest request;

    /**
     * 查询外协单从对应的外协工序的从列表
     */
    @PreAuthorize("@ss.hasPermi('business:outsourceDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizOutsourcedOrderDetailProcess bizOutsourcedOrderDetailProcess) throws NoSuchFieldException, IllegalAccessException
    {
        if(bizOutsourcedOrderDetailProcess.getStartTime()!=null) {
            bizOutsourcedOrderDetailProcess.setStartTime(bizOutsourcedOrderDetailProcess.getStartTime()+ " 00:00:00");
        }
        if(bizOutsourcedOrderDetailProcess.getEndTime()!=null) {
            bizOutsourcedOrderDetailProcess.setEndTime(bizOutsourcedOrderDetailProcess.getEndTime()+ " 23:59:59");
        }
        startPage();
        bizOutsourcedOrderDetailProcess.setSupplierId(Long.valueOf(request.getHeader("tenantId")));
        List<BizOutsourcedOrderDetailProcess> list = bizOutsourcedOrderDetailProcessService.getList(bizOutsourcedOrderDetailProcess);
        return getDataTable(list);
    }

    /**
     * 导出外协单从对应的外协工序的从列表
     */
    @PreAuthorize("@ss.hasPermi('business:outsourceDetail:export')")
    @Log(title = "外协单从对应的外协工序的从", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizOutsourcedOrderDetailProcess bizOutsourcedOrderDetailProcess) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizOutsourcedOrderDetailProcess);
        List<BizOutsourcedOrderDetailProcess> list = bizOutsourcedOrderDetailProcessService.getList(bizOutsourcedOrderDetailProcess);
        ExcelUtil<BizOutsourcedOrderDetailProcess> util = new ExcelUtil<>(BizOutsourcedOrderDetailProcess.class);
        util.exportExcel(response, list, "外协单从对应的外协工序的从数据");
    }
    /**
     * 修改外协单从对应的外协工序的合格数和投产数
     */
    @PreAuthorize("@ss.hasPermi('business:outsourceDetail:edit')")
    @PostMapping(value = "/editOutsourcedOrderDetailProcess")
    public AjaxResult editOutsourcedOrderDetailProcess(@RequestBody BizOutsourcedOrderDetailProcess bizOutsourcedOrderDetailProcess)
    {
        //保存时添加状态
        bizOutsourcedOrderDetailProcess.setStatus(1);
        return success(bizOutsourcedOrderDetailProcessService.updateById(bizOutsourcedOrderDetailProcess));
    }
    /**
     * 查询收回单明细
     */
    @PostMapping(value = "/getOrderDetailProcess")
    public AjaxResult getOrderDetailProcess(@RequestBody BizOutsourcedOrderDetailProcess bizOutsourcedOrderDetailProcess)
    {
        List<BizOutsourcedOrderDetailProcess> list = bizOutsourcedOrderDetailProcessService.getDetailList(bizOutsourcedOrderDetailProcess);
        list.forEach(detail -> {
            // 将字符串拆分转为对象
            List<BizCarrier> carriers = new ArrayList<>();
            // 因为需要显示箱号，所以需要拆分出code重新拼接到NewCarrier
            List<String> codeList = new ArrayList<>();
            // 为了减少前段的逻辑，所以需要先将拆分后的id也拼接起来
            List<Long> carrierIds = new ArrayList<>();
            //使用 StringTokenizer 来分割字符串
            StringTokenizer tokenizer = new StringTokenizer(detail.getNewCarrier(), ",");
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                // 检查当前 token 是否包含 "|" 字符 如果没有表示不是新箱，则跳过当前 token
                if (token.contains("|")) {
                    String[] idCode = token.split("\\|"); // 安全地分割 token
                    if (idCode.length == 2) {
                        codeList.add(idCode[1]);
                        carrierIds.add(Long.parseLong(idCode[0]));
                        BizCarrier bizCarrier = new BizCarrier();
                        bizCarrier.setId(Long.parseLong(idCode[0]));
                        bizCarrier.setCode(idCode[1]);
                        carriers.add(bizCarrier);
                    }
                }
            }
            if(codeList.size()>0){
                detail.setNewCarrier(String.join(",", codeList));
            }
        });
        return success(list);
    }

    /**
     * 获取外协单从对应的外协工序的从详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:outsourceDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizOutsourcedOrderDetailProcessService.getById(id));
    }
}
