package com.sifang.modules.supply.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.order.request.AuditOrderReq;
import com.sifang.modules.supply.order.request.PlaceOrderReq;
import com.sifang.modules.supply.order.request.TraderOrderReq;
import com.sifang.modules.supply.order.resp.TraderWarehouse;
import com.sifang.modules.supply.order.service.MaterialApplyService;
import com.sifang.modules.supply.order.service.TraderOrderService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * 代理商订单
 */
@Controller
@RequestMapping("${adminPath}/material/order")
public class TraderOrderController extends BaseExceptionController {

    @Autowired
    TraderOrderService traderOrderService;

    @Autowired
    MaterialApplyService materialApplyService;

    @RequestMapping("/traderOrder")
    public String traderOrder(Model model) {
        //判断平台员工角色 5.业务人员 6.运管部 7.物资专员 2.项目工程师（工程部）
        User user = UserUtils.getUser();
        String userType = user.getUserType();
        if (UserConstants.TYPE_PLAT_EMP.equals(userType)) {
            model.addAttribute("roleType", traderOrderService.findRoleType(user.getId()));
        }
        model.addAttribute("userType", userType);
        return "modules/supply/order/traderOrderList";
    }

    /**
     * 判断是否可进行操作
     */
    @ResponseBody
    @RequestMapping("/canOperate")
    public Result canOperate(String orderId) {
        boolean result = traderOrderService.checkCanAudit(orderId, Operator.of(UserUtils.getUser()));
        Result<Boolean> ret = new Result<>();
        ret.setCode("200");
        ret.setData(result);
        return ret;
    }

    /**
     * 去审批页面
     */
    @RequestMapping("/toAudit")
    public String toAudit(String applyId, Model model) {
        Record apply = materialApplyService.getRecordById(applyId, Operator.of(UserUtils.getUser()));
        model.addAttribute("apply", apply);
        List<Record> applyDetails = materialApplyService.listDetailsByApplyId(applyId);
        model.addAttribute("applyDetails", applyDetails);
        List<Record> gatherDetails = materialApplyService.listGatherByApplyId(applyId);
        model.addAttribute("gatherDetails", gatherDetails);
        return "modules/supply/order/auditOrder";
    }

    /**
     * 审批、通过or不通过
     */
    @ResponseBody
    @RequestMapping("/auditOrder")
    public Result auditOrder(@RequestBody AuditOrderReq req){
        traderOrderService.auditOrder(req, Operator.of(UserUtils.getUser()));
        return Result.ok();
    }

    /**
     * 去匹配页面
     */
    @RequestMapping("/toMatch")
    public String toMatch(String applyId, Model model) {
        Record apply = materialApplyService.getRecordById(applyId, Operator.of(UserUtils.getUser()));
        model.addAttribute("apply", apply);
        List<Record> applyDetails = materialApplyService.listDetailsByApplyId(applyId);
        model.addAttribute("applyDetails", applyDetails);
        return "modules/supply/order/matchOrder";
    }

    /**
     * 拆单
     */
    @ResponseBody
    @RequestMapping("/splitOrder")
    public Result splitOrder(@RequestBody TraderOrderReq req){
        traderOrderService.splitOrder(req, Operator.of(UserUtils.getUser()));
        return Result.ok();
    }

    /**
     * 并单
     */
    @ResponseBody
    @RequestMapping("/mergeOrder")
    public Result mergeOrder(String orderIds){
        traderOrderService.mergeOrder(orderIds, Operator.of(UserUtils.getUser()));
        return Result.ok();
    }

    /**
     * 下单前获取所有满足库存条件的仓库和下单对象
     */
    @ResponseBody
    @RequestMapping("/canOrderData")
    public List<TraderWarehouse> canOrderData(@Valid @RequestBody TraderOrderReq req, String excludeWarehouseIds){
        return traderOrderService.canOrderData(req, excludeWarehouseIds);
    }

    /**
     * 下单
     */
    @ResponseBody
    @RequestMapping("/placeOrder")
    public Result placeOrder(@Valid @RequestBody PlaceOrderReq req){
        traderOrderService.placeOrder(req, Operator.of(UserUtils.getUser()));
        return Result.ok();
    }
}
