package com.bohuikeji.delivery.backstage.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCustomerBig;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TOrderTemplate;
import com.bohuikeji.delivery.backstage.service.ITCustomerBigService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.ITOrderTemplateService;
import com.bohuikeji.frame.module.admin.annotation.InitLog;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.base.InterfaceBase;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysError;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 订单
 *
 * @author 10552
 */
@Slf4j
@RestController
@RequestMapping("/admin/order/template")
public class OrderTemplateController extends ControllerBase {

    @Autowired
    ITOrderTemplateService templateService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    ITCustomerBigService bigService;

    /**
     * 后台订单新增或修改提交
     * @param params
     * @return
     */
    @InitLog(value = "订单模板增改",action = "增改")
    @PostMapping("/table/model/form")
    public InterfaceBase formSubmit(@RequestParam Map<String, String> params) {
        TOrderTemplate template = (TOrderTemplate) crateEntityByMap(params);
        //新增模板设置默认属性
        if(template.getTemplateId()==null){
            //获取当前登陆用户
            SysUser user=getLoginUser();
            template.setTemplateNum("T"+NumberUtils.getShort());
            template.setCreateUser(user.getUserId());
            template.setFranId(user.getFranId());
            template.setVehicleName(configService.getById(template.getVehicleConfigId()).getConfigName());
            template.setCargoName(configService.getById(template.getCargoConfigId()).getConfigName());
            template.setWorthName(configService.getById(template.getWorthConfigId()).getConfigName());
            template.setWayName(configService.getById(template.getWayConfigId()).getConfigName());
            template.setPaymentType(configService.getById(template.getPaymentConfigId()).getConfigName());

        }
        log.info("这是一个订单模板：{}",template);
        if (template.insertOrUpdate()) {
            return APIEntity.success();
        }
        return APIEntity.fail("系统繁忙，请稍后再试！");
    }

    /**
     * 获取我的订单模板
     * @return
     */
    @RequestMapping("/get/list")
    public APIEntity getList(){
        SysUser user=getLoginUser();
        //如果是大客户账号则只能查看自己的模板
        if(user.getRoleId().equals(17)){
            List<TCustomerBig> bigList=bigService.list(new QueryWrapper<TCustomerBig>().eq("user_id",user.getId()));
            if(bigList.size()==0){
                return APIEntity.fail("您的账号尚未绑定签约客户信息，请联系合作商查询！");
            }
            if(bigList.size()>1){
                SysError.saveError("【大客户账号绑定异常】，同一user_id={1}匹配多个big_id",user.getUserId());
                return APIEntity.fail("检测到您的账号绑定了多个客户信息，请联系合作商核实！");
            }
            return APIEntity.success(templateService.list(new QueryWrapper<TOrderTemplate>().eq("fran_id",user.getFranId()).eq("big_id",bigList.get(0).getBigId())));
        }
        return APIEntity.success(templateService.list(new QueryWrapper<TOrderTemplate>().eq("fran_id",user.getFranId())));
    }



}
