package com.zfire.mall.manager.logic.trade;

import cn.hutool.core.collection.CollectionUtil;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.trade.TradeLoginOrderBean;
import com.zfire.mall.manager.enums.TradeLoginOrderTypeEnum;
import com.zfire.mall.manager.enums.TradeOrderStatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginOrderValidLogic {

    /**
     * 保存参数校验
     * @param adminUser
     * @param bean
     * @return
     */
    public TradeLoginOrderBean addParamsValid(AdminUserCom adminUser, TradeLoginOrderBean bean) {
        bean.setSalesCompanyName(null)
                .setAdminCompanyId(adminUser.getAdminCompanyId())
                .setAdminWebsitId(adminUser.getAdminWebsitId())
                .setAdminUserId(adminUser.getAdminUserId())
                .setOrderStatus(TradeOrderStatusEnum.SAVE.getKey())
                .setLoginStatus("");
        this.commonParamsValid(adminUser, bean);

        return bean;
    }

    /**
     * 编辑参数校验
     * @param adminUser
     * @param bean
     * @return
     */
    public TradeLoginOrderBean editParamsValid(AdminUserCom adminUser, TradeLoginOrderBean bean) {
        this.commonParamsValid(adminUser, bean);

        return bean;
    }

    private TradeLoginOrderBean commonParamsValid(AdminUserCom adminUser, TradeLoginOrderBean bean) {
        if (StringUtils.isBlank(bean.getProjectArea())) {
            throw new RemoteServiceException(bean.getIsSpan() ? "业务所在区域不能为空" : "项目所在区域不能为空");
        }
        if (StringUtils.isBlank(bean.getServiceId())
                && StringUtils.isBlank(bean.getServiceNumber())
                && StringUtils.isEmpty(bean.getServiceName())) {
            throw new RemoteServiceException("业务员不能为空");
        }
        if (bean.getOrderType().equals(TradeLoginOrderTypeEnum.WORK.getKey()) && StringUtils.isBlank(bean.getPartyA())) {
            throw new RemoteServiceException("甲方不能为空");
        }
        if (StringUtils.isBlank(bean.getCustomerId())) {
            throw new RemoteServiceException("经销商不能为空");
        }
        if (StringUtils.isBlank(bean.getCustomerLinkName())) {
            throw new RemoteServiceException("经销商联系人不能为空");
        }
        if (StringUtils.isBlank(bean.getCustomerLinkMobile())) {
            throw new RemoteServiceException("经销商联系电话不能为空");
        }
        if (StringUtils.isBlank(bean.getFollowCustomer())) {
            throw new RemoteServiceException("跟进经销商不能为空");
        }
        if (StringUtils.isBlank(bean.getFollowCustomerMobile())) {
            throw new RemoteServiceException("跟进经销商电话不能为空");
        }
        if (StringUtils.isBlank(bean.getProjectName())) {
            throw new RemoteServiceException("工程项目名称不能为空");
        }
        if (bean.getOrderType().equals(TradeLoginOrderTypeEnum.HOME.getKey()) && StringUtils.isBlank(bean.getHomeProjectNameRadio())) {
            throw new RemoteServiceException("工程项目名称选项不能为空");
        }
        if (StringUtils.isBlank(bean.getEnginLinkName())) {
            throw new RemoteServiceException("工程联系人不能为空");
        }
        if (StringUtils.isBlank(bean.getEnginLinkMobile())) {
            throw new RemoteServiceException("工程联系电话不能为空");
        }
        if (StringUtils.isBlank(bean.getProvinceId())) {
            throw new RemoteServiceException("省不能为空");
        }
        if (StringUtils.isBlank(bean.getCityId())) {
            throw new RemoteServiceException("市不能为空");
        }
        if (StringUtils.isBlank(bean.getAreaId())) {
            throw new RemoteServiceException("区不能为空");
        }
        if (!"1608754035946549250".equals(bean.getCityId())) {
            if (!bean.getIsSpan() && !"1608754006791942145".equals(bean.getCityId()) && StringUtils.isBlank(bean.getStreetId())) {
                throw new RemoteServiceException("街道不能为空");
            }
        }
        if (StringUtils.isBlank(bean.getAddress())) {
            throw new RemoteServiceException("详细地址不能为空");
        }
        if (StringUtils.isBlank(bean.getJoinAddress())) {
            throw new RemoteServiceException("拼接详细地址不能为空");
        }
//        if (!bean.getIsSpan() && StringUtils.isBlank(bean.getPositionAddress())) {
//            throw new RemoteServiceException("定位地址不能为空");
//        }
//        if (!bean.getIsSpan() && StringUtils.isBlank(bean.getLat())) {
//            throw new RemoteServiceException("纬度不能为空");
//        }
//        if (!bean.getIsSpan() && StringUtils.isBlank(bean.getLnt())) {
//            throw new RemoteServiceException("经度不能为空");
//        }
        if (StringUtils.isBlank(bean.getProjectCategory())) {
            throw new RemoteServiceException("项目类别不能为空");
        }
        if (StringUtils.isBlank(bean.getType())) {
            throw new RemoteServiceException("类型不能为空");
        }
        if (StringUtils.isBlank(bean.getExtent())) {
            throw new RemoteServiceException("建筑面积不能为空");
        }
        if (StringUtils.isBlank(bean.getUseExtent())) {
            throw new RemoteServiceException("空调使用面积不能为空");
        }
        if (StringUtils.isBlank(bean.getTradeParentId())) {
            throw new RemoteServiceException("项目所在行业分类不能为空");
        }
        if (StringUtils.isBlank(bean.getTradeId())) {
            throw new RemoteServiceException("行业细分不能为空");
        }
        if (StringUtils.isBlank(bean.getMachineType())) {
            throw new RemoteServiceException("机组类型不能为空");
        }
        if (StringUtils.isBlank(bean.getSuccessRate())) {
            throw new RemoteServiceException("成功机率不能为空");
        }
        if (Objects.isNull(bean.getPreSignDate())) {
            throw new RemoteServiceException("预计签定合同日期不能为空");
        }
        if (Objects.isNull(bean.getPreDeviceAmount())) {
            throw new RemoteServiceException("预计设备金额不能为空");
        }
        if (StringUtils.isBlank(bean.getStatus())) {
            throw new RemoteServiceException("工程跟进状态不能为空");
        }
        if (bean.getIsSpan() && StringUtils.isBlank(bean.getOtherInfo())) {
            throw new RemoteServiceException("工程概况及此工程相关的社会关系以及其他信息不能为空");
        }
        if (CollectionUtil.isEmpty(bean.getItems())) {
            throw new RemoteServiceException("机型明细不能为空");
        }

        return bean;
    }
}
