package com.fjhx.stock.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.base.Condition;
import com.fjhx.entity.material.Material;
import com.fjhx.entity.stock.StockBack;
import com.fjhx.entity.stock.StockBackCorrect;
import com.fjhx.entity.stock.StockBackDetails;
import com.fjhx.material.service.MaterialService;
import com.fjhx.params.stock.CheckDetailsVo;
import com.fjhx.params.stock.StatisticsResult;
import com.fjhx.params.stock.StockBackDetailsVo;
import com.fjhx.params.stock.SubmitBackVo;
import com.fjhx.stock.service.StockBackCorrectService;
import com.fjhx.stock.service.StockBackDetailsService;
import com.fjhx.stock.service.StockBackService;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 退仓面料 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/stockBack")
public class StockBackController {

    @Autowired
    private StockBackService stockBackService;

    @Autowired
    private StockBackDetailsService stockBackDetailsService;

    @Autowired
    private StockBackCorrectService stockBackCorrectService;

    @Autowired
    private MaterialService materialService;

    /**
     * 获取今日领料
     */
    @PostMapping("/getPickingToday")
    public R getPickingToday(@RequestBody Map<String, String> condition) {
        List<Map<String, Object>> result = stockBackService.getPickingToday(condition);
        return R.success(result);
    }

    /**
     * 获取今日领料
     */
    @PostMapping("/getPickingTodayByUserId")
    public R getPickingTodayByUserId(@RequestBody Map<String, String> condition) {
        List<Map<String, Object>> result = stockBackService.getPickingTodayByUserId(condition);
        return R.success(result);
    }

    /**
     * 提交退料
     */
    @PostMapping("/submitBack")
    public R submitBack(@RequestBody SubmitBackVo submitBackVo) {
        stockBackService.submitBack(submitBackVo);
        return R.success();
    }

    /**
     * 退仓复核列表
     */
    @PostMapping("/checkList")
    public R checkList() {
        List<Map<String, Object>> result = stockBackService.checkList();
        return R.success(result);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Map<String, Long> condition) {
        Long stockBackId = condition.get("stockBackId");
        stockBackService.delete(stockBackId);
        return R.success();
    }

    /**
     * 退仓复核详情
     */
    @PostMapping("/checkDetails")
    public R checkDetails(@RequestBody Map<String, Long> condition) {
        Long stockBackId = condition.get("stockBackId");
        CheckDetailsVo result = stockBackService.checkDetails(stockBackId);
        return R.success(result);
    }

    /**
     * 提交退仓复核信息
     */
    @PostMapping("/submitCheck")
    public R submitCheckDetails(@RequestBody CheckDetailsVo checkDetailsVo) {
        List<StockBackDetails> stockBackDetailsList = stockBackService.submitCheckDetails(checkDetailsVo);
        return R.success(stockBackDetailsList);
    }

    /**
     * 面料修正提交
     */
    @PostMapping("/correctSave")
    public R correctSave(@RequestBody StockBackCorrect stockBackCorrect) {
        stockBackCorrect.setCreateTime(new Date());
        stockBackCorrectService.save(stockBackCorrect);
        return R.success();
    }

    /**
     * 面料使用率用户统计
     */
    @PostMapping("/userStatistics")
    public R userStatistics(@RequestBody Map<String, String> condition) {
        Map<String, List<Map<String, Object>>> result = stockBackService.userStatistics(condition);
        return R.success(result);
    }

    /**
     * 面料使用率工艺统计
     */
    @PostMapping("/techStatistics")
    public R techStatistics(@RequestBody Map<String, String> condition) {
        Map<String, Object> result = stockBackService.techStatistics(condition);
        return R.success(result);
    }

    /**
     * 面料使用率用户统计
     */
    @PostMapping("/statisticsDetails")
    public R statisticsDetails(@RequestBody Map<String, String> condition) {
        StatisticsResult result = stockBackService.statisticsDetails(condition);
        return R.success(result);
    }

    /**
     * 面料使用率用户统计
     */
    @PostMapping("/statisticsPage")
    public R statisticsPage(@RequestBody Map<String, String> condition) {
        Page<StatisticsResult> result = stockBackService.statisticsPage(condition);
        return R.success(result);
    }

    /**
     * 面料退仓看板用户列表
     */
    @PostMapping("/getBackUser")
    public R getBackUser(@RequestBody Map<String, String> condition) {
        Map<String, List<Map<String, Object>>> result = stockBackService.getUser(condition);
        return R.success(result);
    }

    /**
     * 面料退仓看板统计
     */
    @PostMapping("/getBackStatistics")
    public R getBackStatistics(@RequestBody Map<String, String> condition) {
        Map<String, Object> result = stockBackService.getBackStatistics(condition);
        return R.success(result);
    }

    /**
     * 面料退仓看板统计
     */
    @PostMapping("/getBackPage")
    public R getBackPage(@RequestBody Map<String, String> condition) {
        Page<Map<String, Object>> result = stockBackService.getBackPage(condition);
        return R.success(result);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody StockBackDetailsVo vo) {
        StockBackDetails stockBackDetails = stockBackDetailsService.getById(vo.getId());
        if (stockBackDetails.getFlowStatus() == 0) {
            throw new ServiceException("节点正在审批中，不可重复发起");
        }
        stockBackDetails.setFlowStatus(0);
        stockBackDetails.setFlowData(JSONObject.toJSONString(vo));
        stockBackDetailsService.updateById(stockBackDetails);
        return R.success();
    }

    @PostMapping("/getFlowData")
    public R flow(@RequestBody StockBackDetailsVo vo) {
        StockBackDetails stockBackDetails = stockBackDetailsService.getById(vo.getId());

        StockBackDetailsVo stockBackDetailsVo = JSONObject.parseObject(stockBackDetails.getFlowData(), StockBackDetailsVo.class);
        String materialId = stockBackDetailsVo.getMaterialId();
        if (materialId != null) {
            Material material = materialService.getById(materialId);
            stockBackDetailsVo.setMaterialName(material.getName());
            stockBackDetailsVo.setWidth(material.getWidth());
        }
        return R.success(stockBackDetailsVo);
    }

    @PostMapping("/handleFlow")
    public R handleFlow(@RequestBody StockBackDetailsVo vo) {
        StockBackDetails stockBackDetails = stockBackDetailsService.getById(vo.getId());

        stockBackDetails.setFlowStatus(1);

        // 审批通过
        if (vo.getFlowResult() == 1) {

            StockBackDetailsVo flowData = JSONObject.parseObject(stockBackDetails.getFlowData(), StockBackDetailsVo.class);
            stockBackDetails.setMaterialId(flowData.getMaterialId());
            stockBackDetails.setMaterialMeters(flowData.getMaterialMeters());

            if (flowData.getOperationTime() != null) {
                stockBackService.update(Wrappers.<StockBack>lambdaUpdate()
                        .eq(StockBack::getId, stockBackDetails.getStockBackId())
                        .set(StockBack::getOperationTime, flowData.getOperationTime()));
            }
        }
        stockBackDetailsService.updateById(stockBackDetails);

        return R.success();
    }

    /**
     * 领料
     */
    @PostMapping("/picking")
    public R picking(@RequestBody Condition condition) {
        List<Map<String, Object>> result = stockBackDetailsService.picking(condition);
        return R.success(result);
    }

    /**
     * 退料
     */
    @PostMapping("/returnMaterial")
    public R returnMaterial(@RequestBody Condition condition) {
        List<Map<String, Object>> result = stockBackDetailsService.returnMaterial(condition);
        return R.success(result);
    }

    /**
     * 修正
     */
    @PostMapping("/correct")
    public R correct(@RequestBody Condition condition) {
        List<Map<String, Object>> result = stockBackDetailsService.correct(condition);
        return R.success(result);
    }


    @GetMapping("/setStatisticsDate")
    public R setStatisticsDate() {
        List<StockBack> list = stockBackService.list(
                Wrappers.<StockBack>lambdaQuery().select(StockBack::getId, StockBack::getOperationTime));
        for (StockBack stockBack : list) {
            Date date = stockBack.getOperationTime();

            if (date == null) {
                continue;
            }

            if (Convert.toInt(DateUtil.format(date, "H")) < 10) {
                stockBack.setStatisticsDate(DateUtil.offsetDay(date, -1));
            } else {
                stockBack.setStatisticsDate(date);
            }
        }
        stockBackService.updateBatchById(list);
        return R.success();
    }

}

