package com.ruoyi.basicData.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basicData.domain.TCorps;
import com.ruoyi.basicData.domain.TCustomerContact;
import com.ruoyi.basicData.mapper.TCorpsMapper;
import com.ruoyi.basicData.mapper.TCustomerContactMapper;
import com.ruoyi.basicData.service.ITCorpsService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.mapper.TWarehouseBillsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * 客户详情Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@Service
public class TCorpsServiceImpl implements ITCorpsService {
    @Autowired
    private TCorpsMapper tCorpsMapper;

    @Autowired
    private TCustomerContactMapper tCustomerContactMapper;

    @Autowired
    private TWarehouseBillsMapper tWarehouseBillsMapper;

    /**
     * 查询客户详情
     *
     * @param fId 客户详情ID
     * @return 客户详情
     */
    @Override
    @Transactional
    public Map<String, Object> selectTCorpsById(Long fId) {
        Map<String, Object> map = new HashMap<>();
        TCorps tCorps = tCorpsMapper.selectTCorpsById(fId);
        if (StringUtils.isNotNull(tCorps)) {
            map.put("corp", tCorps);
        }
       // List<TCustomerContact> customerContactList = tCustomerContactMapper.selectList(new EntityWrapper<TCustomerContact>().eq("f_pid", fId));
        TCustomerContact tCustomerContact=  new TCustomerContact();
        tCustomerContact.setfPid(fId);
        List<TCustomerContact> customerContactList =tCustomerContactMapper.selectTCustomerContactList(tCustomerContact);
        if (StringUtils.isNotEmpty(customerContactList)) {
            map.put("customerContact", customerContactList);
        }
        return map;
    }

    /**
     * 查询客户详情列表
     *
     * @param tCorps 客户详情
     * @return 客户详情
     */
    @Override
    public List<TCorps> selectTCorpsList(TCorps tCorps) {
        return tCorpsMapper.selectTCorpsList(tCorps);
    }

    /**
     * 新增客户详情
     *
     * @param tCorps            客户详情
     * @param tCustomerContacts 客户联系人
     * @param loginUser         当前登陆人
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertTCorps(String tCorps, String tCustomerContacts, LoginUser loginUser) {
        if (StringUtils.isEmpty(tCorps)) {
            return AjaxResult.error("提交失败：客户信息为空");
        }
        if (StringUtils.isEmpty(tCustomerContacts)) {
            return AjaxResult.error("提交失败：客户联系人为空");
        }
        Long fPid = null;
        TCorps corps = JSONArray.parseObject(tCorps, TCorps.class);
        JSONArray jsonArray = JSONArray.parseArray(tCustomerContacts);
        List<TCustomerContact> customerContactList = JSONObject.parseArray(jsonArray.toJSONString(), TCustomerContact.class);
        if (StringUtils.isNull(corps.getfId())) {
            corps.setCreateBy(loginUser.getUser().getUserName());
            corps.setCreateTime(new Date());
            corps.setfStatus("0");
            tCorpsMapper.insertTCorps(corps);
            fPid=corps.getfId();
        } else {
            fPid=corps.getfId();
            corps.setUpdateBy(loginUser.getUser().getUserName());
            corps.setUpdateTime(new Date());
            if(corps.getfStatus().equals("1")){
                TWarehouseBills tWarehouseBill =new TWarehouseBills();
                tWarehouseBill.setfCorpid(corps.getfId());
                List<TWarehouseBills>   tWarehouseBillList=tWarehouseBillsMapper.selectTWarehousebillsList(tWarehouseBill);
                if(tWarehouseBillList!=null && !tWarehouseBillList.isEmpty()){
                    return AjaxResult.error("客户在库存总账有记录不可变更状态");
                }
            }
            tCorpsMapper.updateTCorps(corps);
            tCustomerContactMapper.deleteCustomerContactByCorps(fPid);
        }
        for (TCustomerContact cc : customerContactList) {
            cc.setfPid(fPid);
            cc.setfStatus("0");
            cc.setCreateBy(loginUser.getUser().getUserName());
            cc.setCreateTime(new Date());
            tCustomerContactMapper.insertTCustomerContact(cc);
        }
        return AjaxResult.success();
    }

    /**
     * 修改客户详情
     *
     * @param tCorps 客户详情
     * @return 结果
     */
    @Override
    public AjaxResult updateTCorps(TCorps tCorps) {
        TWarehouseBills tWarehouseBill =new TWarehouseBills();
        tWarehouseBill.setfCorpid(tCorps.getfId());
        List<TWarehouseBills>   tWarehouseBillList=tWarehouseBillsMapper.selectTWarehousebillsList(tWarehouseBill);
        if(tWarehouseBillList!=null && !tWarehouseBillList.isEmpty()){
            return AjaxResult.error("客户在库存总账有记录不可变更状态");
        }
        tCorps.setUpdateTime(DateUtils.getNowDate());
        tCorpsMapper.updateTCorps(tCorps);
        return AjaxResult.success();
    }

    /**
     * 批量删除客户详情
     *
     * @param fIds 需要删除的客户详情ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteTCorpsByIds(Long[] fIds) {
        //return tCorpsMapper.deleteTCorpsByIds(fIds);
        // 查询仓库主表 是否有客户存在
        int i = 1;
        for(Long id:fIds){
            TWarehouseBills tWarehouseBill =new TWarehouseBills();
            tWarehouseBill.setfCorpid(id);
            List<TWarehouseBills>   tWarehouseBillList=tWarehouseBillsMapper.selectTWarehousebillsList(tWarehouseBill);
            if(tWarehouseBillList!=null && !tWarehouseBillList.isEmpty()){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("客户第" + i + "行仓库有货物不可删除");
            }
            tCorpsMapper.deleteTCorpsById(id);
            i++;
        }
        return AjaxResult.success();
    }

    /**
     * 删除客户详情信息
     *
     * @param fId 客户详情ID
     * @return 结果
     */
    @Override
    public int deleteTCorpsById(Long fId) {
        return tCorpsMapper.deleteTCorpsById(fId);
    }

    @Override
    public String checkUFNoUnique(TCorps tCorps) {
        TCorps tCorps1 = tCorpsMapper.checkFNoUnique(tCorps.getfNo());
        if (StringUtils.isNotNull(tCorps1) && tCorps1.getfId()!=tCorps.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkUFNnameUnique(TCorps tCorps) {
        TCorps tCorps1 = tCorpsMapper.checkUFNnameUnique(tCorps.getfName());
        if (StringUtils.isNotNull(tCorps1) && tCorps1.getfId()!=tCorps.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkUFCnameUnique(TCorps tCorps) {
        TCorps tCorps1 = tCorpsMapper.checkUFCnameUnique(tCorps.getfCname());
        if (StringUtils.isNotNull(tCorps1) && tCorps1.getfId()!=tCorps.getfId()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
