package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.mapper.CustomerInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.ICustomerInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.service.IPurchaseOrderService;
import com.yh.saas.plugin.yiliangyiyun.service.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-07-21
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {

    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private IPurchaseOrderService purchaseOrderService;
    @Autowired
    private ISaleOrderService saleOrderService;

    /**
     * 客户管理列表
     *
     * @param customerInfo
     * @return
     */
    @Override
    public Page<CustomerInfo> selectCustomerInfo(CustomerInfo customerInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (customerInfo.getCurrentPage() - 1)
                * customerInfo.getPageSize());
        //公司id
        pageView.put("compId", customerInfo.getCompId());
        pageView.put("searchKeyWord", customerInfo.getSearchKeyWord());
        pageView.put("pageSize", customerInfo.getPageSize());
        pageView.put("currentPage", customerInfo.getCurrentPage());
        // 查询客户总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<CustomerInfo> dataList = baseMapper.getListByCondition(pageView);
        Page<CustomerInfo> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(customerInfo.getCurrentPage());
        page.setSize(customerInfo.getPageSize());
        return page;
    }


    /**
     * 增加\退回定金
     *
     * @param customerInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editCustomerInfo(CustomerInfo customerInfo) {
        //查询客户信息
        CustomerInfo customerInfo1 = this.selectById(customerInfo.getId());
        if (customerInfo1 != null) {
            //0 修改定金比例
            if (customerInfo.getFlag() == 0) {
                customerInfo1.setDepositRatio(customerInfo.getDepositRatio());
                //查询采购订单表信息
                Wrapper<PurchaseOrder> purchaseOrderWrapper = new EntityWrapper<>();
                purchaseOrderWrapper.andNew().eq("customer", customerInfo1.getCustomerName()).or()
                        .eq("customer", customerInfo1.getCompName());
                purchaseOrderWrapper.eq("delete_flag", "0");
                List<PurchaseOrder> purchaseOrderList = purchaseOrderService.selectList(purchaseOrderWrapper);
                if (!CollectionUtils.isEmpty(purchaseOrderList)) {
                    for (PurchaseOrder purchaseOrder : purchaseOrderList) {
                        //更改订单表拖欠定金
                        //拖欠定金=交易量x定金比例-已冻结定金
                        purchaseOrder.setDefaultDeposit(purchaseOrder.getTransactionsNumber() * customerInfo.getDepositRatio() - purchaseOrder.getFreezingDeposit());
                        purchaseOrderService.updateById(purchaseOrder);
                    }
                }
                //查询销售订单表信息
                Wrapper<SaleOrder> saleOrderWrapper = new EntityWrapper<>();
                saleOrderWrapper.andNew().eq("customer", customerInfo1.getCustomerName()).or()
                        .eq("customer", customerInfo1.getCompName());
                saleOrderWrapper.eq("delete_flag", "0");
                List<SaleOrder> saleOrderList = saleOrderService.selectList(saleOrderWrapper);
                if (!CollectionUtils.isEmpty(saleOrderList)) {
                    for (SaleOrder saleOrder : saleOrderList) {
                        //更改订单表拖欠定金
                        //拖欠定金=交易量x定金比例-已冻结定金
                        saleOrder.setDefaultDeposit(saleOrder.getTransactionsNumber() * customerInfo.getDepositRatio() - saleOrder.getFreezingDeposit());
                        saleOrderService.updateById(saleOrder);
                    }
                }
                //更改客户信息
                this.updateById(customerInfo1);
                return "OK";
            }
            //1 增加定金
            else if (customerInfo.getFlag() == 1) {
                customerInfo1.setTotalDeposit(customerInfo1.getTotalDeposit() + customerInfo.getMoney());
                //更改客户信息
                this.updateById(customerInfo1);
                return "OK";
            }
            //2 退回定金
            else {
                customerInfo1.setTotalDeposit(customerInfo1.getTotalDeposit() - customerInfo.getMoney());
                //更改客户信息
                this.updateById(customerInfo1);
                return "OK";
            }
        }
        return "NG";
    }

    /**
     * 设置默认定金比例
     *
     * @param compId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void installDepositRatio(String compId, Float depositRatio) {
        //查询该公司所有客户
        List<CustomerInfo> customerInfoList = customerInfoService.selectList(new EntityWrapper<CustomerInfo>()
                .eq(CustomerInfo.QueryFiles.COMP_ID, compId)
                .eq(WarehouseBaseInfo.QueryFiles.DELETE_FLAG, NumberConstant.CONSTANT0));
        if (!CollectionUtils.isEmpty(customerInfoList)) {
            for (CustomerInfo customerInfo : customerInfoList) {
                //修改定金比例
                customerInfo.setDepositRatio(depositRatio);
                customerInfoService.updateById(customerInfo);
            }
        }
    }

    /**
     * 查看客户信息
     *
     * @param id
     */
    @Override
    public CustomerInfo getCustomerInfo(String id) {
        //查看客户信息
        CustomerInfo customerInfo = this.selectById(id);
        return customerInfo;
    }

    /**
     * 添加客户信息
     *
     * @param customerInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addCustomerInfo(CustomerInfo customerInfo) {
        //新增主键id
        customerInfo.setId(IdGenerator.generateUUID());
        // 操作主表数据
        boolean one = this.insert(customerInfo);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 编辑客户信息
     *
     * @param customerInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String modifyCustomerInfo(CustomerInfo customerInfo) {
        boolean one = this.updateById(customerInfo);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteCustomerInfo(String id) {
        CustomerInfo customerInfo=this.selectById(id);
        this.deleteById(customerInfo.getId());
    }

    /**
     * 客户下拉列表
     * @param compId
     * @return
     */
    @Override
    public List<CustomerInfo> customerList(String compId) {
        List<CustomerInfo> customerInfoList=this.selectList(new EntityWrapper<CustomerInfo>()
        .eq("comp_id",compId).eq("delete_flag", NumberConstant.CONSTANT0));
        return customerInfoList;
    }

}
