package com.scm.contract.web.apply;

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.service.apply.DictionaryLabelApplicationStatusService;
import com.scm.contract.service.apply.LabelApplicationService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.BaseController;
import com.set.direwolf.core.web.OperationResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * 标签发放
 * @author zuo
 * @date 2024/6/12 11:07
 */
@Controller
@RequestMapping("${adminPath}/tag/distribution")
public class LabelDistributionController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(LabelApplicationController.class);

    private final LabelApplicationService entityService;


    private final DictionaryLabelApplicationStatusService statusService;

    @Autowired
    public LabelDistributionController(LabelApplicationService entityService,DictionaryLabelApplicationStatusService statusService) {
        this.entityService = entityService;
        this.statusService = statusService;
    }

    /**
     * 分页查询数据获取
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @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(8L);
            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 entity
     * @return Page
     */
    @PutMapping("")
    public @ResponseBody OperationResult editMethod(@RequestBody LabelApplication entity) {
        return OperationResult.buildSuccessResult("发货成功", entityService.editShippingMethod(entity));
    }
}
