package com.scm.contract.web.print;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.scm.contract.model.apply.DictionaryLabelApplicationStatus;
import com.scm.contract.model.apply.LabelApplication;
import com.scm.contract.model.print.PrintCode;
import com.scm.contract.service.apply.DictionaryLabelApplicationStatusService;
import com.scm.contract.service.apply.LabelApplicationService;
import com.scm.contract.service.print.PrintCodeService;
import com.scm.contract.service.print.ReprintCodeService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.OperationResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zuo
 * @date 2024/6/26 9:22
 */

@Controller
@RequestMapping("${adminPath}/print/code")
public class PrintCodeController {

    @Autowired
    private LabelApplicationService entityService;

    @Autowired
    private PrintCodeService printCodeService;

    @Autowired
    private DictionaryLabelApplicationStatusService statusService;

    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody LabelApplication entity, HttpServletRequest request, HttpServletResponse response) {
        List<String> tagApplyStateIds = entity.getTagApplyStateIds();
        if (CollectionUtils.isEmpty(tagApplyStateIds)) {
            List<DictionaryLabelApplicationStatus> list = statusService.list(2L);
            List<String> ids = list.stream().map(DictionaryLabelApplicationStatus::getId).distinct().collect(Collectors.toList());
            entity.setTagApplyStateIds(ids);
        }
        Page<LabelApplication> page = Page.bootstrapPage(request, response);
        List<List<String>> orderList = entity.getOrder();
        String defaultOrder = HumpLineAdopter.humpToLine(orderList);
        if (StringUtils.isNotEmpty(defaultOrder)) {
            page.setOrderBy(defaultOrder);
        }
        return OperationResult.buildSuccessResult("获取分页查询信息成功", entityService.findPage(page, entity));
    }

    /**
     * 详情信息
     *
     * @param id
     * @return Page
     */
    @GetMapping("/{id}")
    public @ResponseBody OperationResult getById(@PathVariable("id") String id) {
        return OperationResult.buildSuccessResult("获取详情信息成功", entityService.getById(id, true));
    }


    /**
     * 赋码
     *
     * @param ids
     * @return
     */
    @PostMapping("/coding")
    public @ResponseBody OperationResult coding(@RequestBody List<String> ids) {
        return OperationResult.buildSuccessResult("赋码成功", printCodeService.coding(ids));
    }

    /**
     * 预览信息
     *
     * @param id 标签申领的id
     * @return Page
     */
    @GetMapping("preview/{id}")
    public @ResponseBody OperationResult getPreviewById(@PathVariable("id") String id) {
        return OperationResult.buildSuccessResult("获取预览信息成功", printCodeService.getPreviewById(id));
    }

    /**
     * 打印
     *
     * @param labelApplicationIds
     * @return
     */
    @PostMapping("/send")
    public @ResponseBody OperationResult printSend(@RequestBody List<String> labelApplicationIds) {
        return OperationResult.buildSuccessResult("打印信息发送成功", printCodeService.printSend(labelApplicationIds));
    }

    /**
     * 重新打印
     *
     * @param printCodeIds   需要重新打印的printCodeIds
     * @return               Boolean
     */
    @PostMapping("/reprint/send")
    public @ResponseBody OperationResult reprintSend(@RequestBody List<String> printCodeIds) {
        return OperationResult.buildSuccessResult("重新打印信息发送成功", printCodeService.reprintSend(printCodeIds));
    }

    /**
     * 重新打印导入
     *
     * @param file   导入文件
     * @return        Boolean
     */
    @PostMapping("/reprint/send/import")
    public @ResponseBody OperationResult reprintSendImport(MultipartFile file) {
        return OperationResult.buildSuccessResult("重新打印信息发送成功", printCodeService.reprintSendImport(file));
    }


    /**
     * 根据detailId获取对应的List<PrintCode>(重新打印前，查询可以重新打印的编码信息)
     *
     * @param detailId  标签申领详单id
     * @return          该detailId对应的所有code（List<PrintCode>）信息
     */
    @GetMapping("/list/{detailId}")
    public @ResponseBody OperationResult getCodesByDetailId(@PathVariable("detailId") String detailId) {
        return OperationResult.buildSuccessResult("获取信息成功", printCodeService.getCodesByDetailId(detailId));
    }
}
