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

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.base.service.ICommonBillOperateHisService;
import com.yh.saas.plugin.yiliangyiyun.constant.NumberConstant;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.SalePlanInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.UnimallMessage;
import com.yh.saas.plugin.yiliangyiyun.mapper.SalePlanInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.ISalePlanInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.util.GeTuiUtils;
import com.yh.saas.plugin.yiliangyiyun.util.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 记录销售计划 服务实现类
 * </p>
 *
 * @author Gongdc
 * @since 2021-08-04
 */
@Service
public class SalePlanInfoServiceImpl extends ServiceImpl<SalePlanInfoMapper, SalePlanInfo> implements ISalePlanInfoService {
    @Autowired
    private ICommonBillOperateHisService billOperateHisService;
    @Autowired
    private GeTuiUtils geTuiUtils;
    @Autowired
    private WebSocket webSocket;

    /**
     * 查看销售计划
     *
     * @param id
     * @return
     */
    @Override
    public SalePlanInfo getSalePlanInfo(String id) {
        //查看销售计划信息
        SalePlanInfo salePlanInfo = this.selectById(id);
        return salePlanInfo;
    }

    /**
     * 编辑销售计划
     *
     * @param salePlanInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editSalePlanInfo(SalePlanInfo salePlanInfo) {
        //状态改为待审核
        salePlanInfo.setStatus(StatusEnum.ORDER_REVIEWED.getName());
        salePlanInfo.setStatusFlag(StatusEnum.ORDER_REVIEWED.getFlag());
        boolean one = this.updateById(salePlanInfo);
        if (one) {
            return "OK";
        } else {
            return "NG";
        }
    }

    /**
     * 删除销售计划
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalePlanInfo(String id) {
        //查询销售计划信息
        SalePlanInfo salePlanInfo = this.selectById(id);
        if (salePlanInfo != null) {
            //删除销售计划信息
            this.deleteById(salePlanInfo.getId());
        }
    }

    /**
     * 销售计划列表
     *
     * @param salePlanInfo
     * @return
     */
    @Override
    public Page<SalePlanInfo> selectSalePlanInfo(SalePlanInfo salePlanInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (salePlanInfo.getCurrentPage() - 1)
                * salePlanInfo.getPageSize());
        //公司id
        pageView.put("compId", salePlanInfo.getCompId());
        pageView.put("commonId", salePlanInfo.getCommonId());
        pageView.put("searchKeyWord", salePlanInfo.getSearchKeyWord());
        pageView.put("searchType", salePlanInfo.getSearchType());
        pageView.put("pcFlag", salePlanInfo.getPcFlag());
        pageView.put("showFlag", salePlanInfo.getShowFlag());
        pageView.put("receivePrivate", salePlanInfo.getReceivePrivate());
        pageView.put("pageSize", salePlanInfo.getPageSize());
        pageView.put("currentPage", salePlanInfo.getCurrentPage());
        // 查询销售订单总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<SalePlanInfo> dataList = baseMapper.getListByCondition(pageView);
        Page<SalePlanInfo> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(salePlanInfo.getCurrentPage());
        page.setSize(salePlanInfo.getPageSize());
        return page;
    }

    /**
     * 添加销售计划
     *
     * @param salePlanInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertSalePlanInfo(SalePlanInfo salePlanInfo) {
        //新增主键id
        salePlanInfo.setId(IdGenerator.generateUUID());
        if ("1".equals(salePlanInfo.getPcFlag())) {
            salePlanInfo.setStatus(StatusEnum.ORDER_PASSED.getName());
            salePlanInfo.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
        } else {
            salePlanInfo.setStatus(StatusEnum.ORDER_REVIEWED.getName());
            salePlanInfo.setStatusFlag(StatusEnum.ORDER_REVIEWED.getFlag());
        }
        salePlanInfo.setShowFlag(StatusEnum.PURCHASE_SHOW.getFlag());
        // 操作主表数据
        this.insert(salePlanInfo);
        // 插入操作历史
        String staffName = this.billOperateHisService.getStaffAndName();
        // 插入操作历史
        this.billOperateHisService.saveBillOperateHis(salePlanInfo.getId(), NumberConstant.CONSTANT_PURCHASE, staffName, null,
                salePlanInfo.getStatus(), null, "");
        return salePlanInfo.getId();
    }

    /**
     * 更改状态
     *
     * @param salePlanInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editStatus(SalePlanInfo salePlanInfo) {
        //查询销售计划列表
        SalePlanInfo salePlanInfo1 = this.selectById(salePlanInfo.getId());
        if (salePlanInfo1 != null) {
            //更改状态
            if (salePlanInfo.getFlag() == 0) {
                if (StatusEnum.PURCHASE_HIDE.getFlag().equals(salePlanInfo1.getShowFlag())) {
                    salePlanInfo1.setShowFlag(StatusEnum.PURCHASE_SHOW.getFlag());
                } else {
                    salePlanInfo1.setShowFlag(StatusEnum.PURCHASE_HIDE.getFlag());
                }
                //更改销售计划信息
                this.updateById(salePlanInfo1);
                // 插入操作历史
                String staffName = this.billOperateHisService.getStaffAndName();
                // 插入操作历史
                this.billOperateHisService.saveBillOperateHis(salePlanInfo1.getId(), NumberConstant.CONSTANT_PURCHASE, staffName, null,
                        salePlanInfo1.getStatus(), null, "");
                return "OK";
            }
            //更改基差
            else if (salePlanInfo.getFlag() == 1) {
                salePlanInfo1.setBasisPrice(salePlanInfo.getBasisPrice());
                //更改销售计划信息
                this.updateById(salePlanInfo1);
                return "OK";
            }
            //更改单价
            else {
                salePlanInfo1.setSalePrice(salePlanInfo.getSalePrice());
                //更改销售计划信息
                this.updateById(salePlanInfo1);
                return "OK";
            }
        }
        return "NG";
    }

    /**
     * 审核
     *
     * @param salePlanInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String examine(SalePlanInfo salePlanInfo) {
        //查询销售计划列表
        SalePlanInfo salePlanInfo1 = this.selectById(salePlanInfo.getId());
        if (salePlanInfo1 != null) {
            //通过
            if (salePlanInfo.getFlag() == 1) {
                salePlanInfo1.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
                salePlanInfo1.setStatus(StatusEnum.ORDER_PASSED.getName());
                geTuiUtils.pushByCid("审核通知", "您申请的交易已审核通过", salePlanInfo1.getCommonId());
                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(salePlanInfo1.getId());
                messageDO.setCustomer(salePlanInfo1.getSeller());
                messageDO.setOperation("审核通知");
                messageDO.setResult("您申请的交易已审核通过");
                messageDO.setGmtUpdate(new java.util.Date());
                messageDO.setGmtCreate(new Date());
                messageDO.setTaskId(IdGenerator.generateUUID());
                messageDO.setCompanyId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                messageDO.setPath("XXXXX");
                webSocket.sendOneMessage(messageDO);

            }
            //驳回
            else {
                salePlanInfo1.setStatusFlag(StatusEnum.ORDER_REJECTED.getFlag());
                salePlanInfo1.setStatus(StatusEnum.ORDER_REJECTED.getName());
                geTuiUtils.pushByCid("驳回通知", "您申请的交易已被驳回", salePlanInfo1.getCommonId());

                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(salePlanInfo1.getId());
                messageDO.setCustomer(salePlanInfo1.getSeller());
                messageDO.setOperation("驳回通知");
                messageDO.setResult("您申请的交易已被驳回");
                messageDO.setGmtUpdate(new java.util.Date());
                messageDO.setGmtCreate(new Date());
                messageDO.setTaskId(IdGenerator.generateUUID());
                messageDO.setCompanyId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                messageDO.setPath("XXXXX");
                webSocket.sendOneMessage(messageDO);
            }
            //更改销售计划信息
            this.updateById(salePlanInfo1);
            return "OK";
        }
        return "NG";
    }
}
