package com.jwipc.order.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Strings;
import com.jwipc.constant.DictCode;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.order.dto.DeliveryOrderLookupDTO;
import com.jwipc.order.dto.DeliveryPrintDTO;
import com.jwipc.order.entity.DeliveryOrder;
import com.jwipc.order.logic.DeliveryOrderLogic;
import com.jwipc.order.service.IDeliveryOrderService;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.clent.dto.SupplierDTO;
import com.jwipc.supplier.client.SupplierClient;
import com.jwipc.sys.client.UserClient;
import com.jwipc.sys.client.dto.UserDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Objects;

import static com.jwipc.order.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>送货单相关页面</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/deliveryOrder/page")
@RequiredArgsConstructor
public class DeliveryOrderPageController {
    private final DeliveryOrderLogic deliveryOrderLogic;
    private final SupplierClient supplierClient;
    private final UserClient userClient;
    private final IDeliveryOrderService deliveryOrderService;


    /**
     * 导入送货单创建页面
     *
     * @param model
     * @param overseasFlag 境外送货单标识
     * @return
     */
    @GetMapping("importCreate")
    public String importCreate(Model model,String supplierCode1,Integer overseasFlag)
    {
        DeliveryOrderLookupDTO deliveryOrderBaseDTO1 = new DeliveryOrderLookupDTO();
        if (LoginUtil.isSupplier()) {
            final SupplierDTO supplierByCode = supplierClient.getSupplierByCode(LoginUtil.getSupplierCode());
            deliveryOrderBaseDTO1.setSupplierName(supplierByCode.getSupplierName());
            deliveryOrderBaseDTO1.setSupplierErpCode(supplierByCode.getSupplierErpCode());
            //获取当前供应商送货单的数据
            final DeliveryOrder lastDeliveryOrder = deliveryOrderService.getOne(
                    new LambdaQueryWrapper<DeliveryOrder>().eq(DeliveryOrder::getSupplierErpCode,
                            supplierByCode.getSupplierErpCode()), false);
            if(Objects.nonNull(lastDeliveryOrder)){
                deliveryOrderBaseDTO1.setConsigner(lastDeliveryOrder.getConsigner());
                deliveryOrderBaseDTO1.setPhone(lastDeliveryOrder.getPhone());
            }
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        model.addAttribute("bean", deliveryOrderBaseDTO1);
        model.addAttribute("overseasFlag", overseasFlag);
        return MODULE_ROOT + "/deliveryOrder/import_create";
    }

    /**
     * 送货单创建页面
     *
     * @param model
     * @param deliveryOrderBaseDTO 创建对象
     * @return
     */
    @AuditLogInter(value = "明细界面", module = "送货管理")
    @GetMapping("createOrUpdate")
    public String createOrUpdate(Model model, DeliveryOrderLookupDTO deliveryOrderBaseDTO,
                                 @RequestParam(required = false, defaultValue = "0") int type,
                                 @RequestParam(required = false, defaultValue = "0") Long id,
                                 @RequestParam(required = false, defaultValue = "") String no)
    {
        DeliveryOrderLookupDTO deliveryOrderBaseDTO1;
        if (id == 0) {
            if (!Strings.isNullOrEmpty(no)) {//查询已有的送货单进行修改
                DeliveryOrder deliveryOrder = deliveryOrderLogic.getDeliveryOrderByCode(no);
                id = deliveryOrder.getId();
            }
            deliveryOrderBaseDTO1 = deliveryOrderLogic.getDefaultDetail(deliveryOrderBaseDTO, id);
            final UserDTO userDTO = userClient.getByAccount(deliveryOrderBaseDTO1.getSupplierErpCode());
            final List<String> userRoleKeys = userClient.getUserRoleKeys(userDTO.getId());
            if (userRoleKeys.contains(DictCode.SUPPLIER_SPECIAL_ROLE)) {
                model.addAttribute(DictCode.SUPPLIER_SPECIAL_ROLE, 1);
            }
        } else {
            deliveryOrderBaseDTO1 = deliveryOrderLogic.getDefaultDetail(deliveryOrderBaseDTO, id);
        }
        if(type == 0 && !("4386".equals(deliveryOrderBaseDTO1.getSupplierErpCode()) || "4386E".equals(deliveryOrderBaseDTO1.getSupplierErpCode()))){
            deliveryOrderBaseDTO1.setRemark("");
        }
        deliveryOrderBaseDTO1.setSupplierCode1(deliveryOrderBaseDTO.getSupplierCode1());
        model.addAttribute("bean", deliveryOrderBaseDTO1);
        model.addAttribute("type", type);

        return MODULE_ROOT + "/deliveryOrder/create";
    }

    /**
     * 送货单创建页面
     *
     * @param model
     * @param deliveryOrderBaseDTO 创建对象
     * @return
     */
    @AuditLogInter(value = "编辑界面", module = "送货管理")
    @GetMapping("edit")
    public String createOrUpdate(Model model,
                                 @RequestParam(required = false, defaultValue = "0") Long id)
    {
        DeliveryOrderLookupDTO deliveryOrderBaseDTO1;
        deliveryOrderBaseDTO1 = deliveryOrderLogic.getDefaultDetail(null, id);
        model.addAttribute("bean", deliveryOrderBaseDTO1);
        return MODULE_ROOT + "/deliveryOrder/edit";
    }

    /**
     * 送货单列表页面
     *
     * @return
     */
    @GetMapping("list")
    public String list(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("userCode", LoginUtil.getAccount());
        return MODULE_ROOT + "/deliveryOrder/list";
    }

    /**
     * 送货单列表页面
     *
     * @return
     */
    @GetMapping("attachment")
    public String attachment(Model model, String id,
                             @RequestParam(required = false, defaultValue = "0") Integer batchFlag)
    {
        model.addAttribute("id", id);
        model.addAttribute("batchFlag", batchFlag);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/deliveryOrder/attachment";
    }

    @GetMapping("ftpDialog")
    public String ftpDialog() {
        return MODULE_ROOT + "/deliveryOrder/ftp_dialog";
    }

    /**
     * 送货单打印页面
     *
     * @return
     */
    @GetMapping("print")
    public String list(Model model, String ids) {
        final DeliveryPrintDTO printData = deliveryOrderLogic.getPrintData(ids);
        model.addAttribute("printData", printData);
        return MODULE_ROOT + "/deliveryOrder/print_template";
    }

    @GetMapping("lookReceiveVoucher")
    public String lookReceiveVoucher(Model model, String orderCode) {
        model.addAttribute("headUpText", orderCode);
        return MODULE_ROOT + "/deliveryOrder/receive";
    }
}
