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.ProcurementPlanInfo;
import com.yh.saas.plugin.yiliangyiyun.entity.UnimallMessage;
import com.yh.saas.plugin.yiliangyiyun.entity.view.ReleaseView;
import com.yh.saas.plugin.yiliangyiyun.mapper.ProcurementPlanInfoMapper;
import com.yh.saas.plugin.yiliangyiyun.service.IProcurementPlanInfoService;
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-07-20
 */
@Service
public class ProcurementPlanInfoServiceImpl extends ServiceImpl<ProcurementPlanInfoMapper, ProcurementPlanInfo> implements IProcurementPlanInfoService {
    @Autowired
    private ICommonBillOperateHisService billOperateHisService;
    @Autowired
    private GeTuiUtils geTuiUtils;
    @Autowired
    private WebSocket webSocket;

    /**
     * 查看采购计划
     *
     * @param id
     * @return
     */
    @Override
    public ProcurementPlanInfo getProcurementPlan(String id) {
        //查看采购计划信息
        ProcurementPlanInfo procurementPlanInfo = this.selectById(id);
        return procurementPlanInfo;
    }

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


    /**
     * 删除采购计划
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcurementPlan(String id) {
        //查询采购计划信息
        ProcurementPlanInfo procurementPlanInfo = this.selectById(id);
        if (procurementPlanInfo != null) {
            //删除采购计划信息
            this.deleteById(procurementPlanInfo.getId());
        }
    }

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

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

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

    }

    /**
     * 审核
     *
     * @param procurementPlanInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String examine(ProcurementPlanInfo procurementPlanInfo) {
        //查询采购计划列表
        ProcurementPlanInfo procurementPlanInfo1 = this.selectById(procurementPlanInfo.getId());
        if (procurementPlanInfo1 != null) {
            //通过
            if (procurementPlanInfo.getFlag() == 1) {
                procurementPlanInfo1.setStatusFlag(StatusEnum.ORDER_PASSED.getFlag());
                procurementPlanInfo1.setStatus(StatusEnum.ORDER_PASSED.getName());
                geTuiUtils.pushByCid("审核通知", "您申请的交易已审核通过", procurementPlanInfo1.getCommonId());

                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(procurementPlanInfo1.getCommonId());
                messageDO.setCustomer(procurementPlanInfo1.getBuyer());
                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 {
                procurementPlanInfo1.setStatusFlag(StatusEnum.ORDER_REJECTED.getFlag());
                procurementPlanInfo1.setStatus(StatusEnum.ORDER_REJECTED.getName());
                geTuiUtils.pushByCid("驳回通知", "您申请的交易已被驳回", procurementPlanInfo1.getCommonId());

                UnimallMessage messageDO = new UnimallMessage();
                messageDO.setAdminId(procurementPlanInfo1.getCommonId());
                messageDO.setCustomer(procurementPlanInfo1.getBuyer());
                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(procurementPlanInfo1);
            return "OK";
        }
        return "NG";
    }

    /**
     * 我的发布列表
     *
     * @param procurementPlanInfo
     * @return
     */
    @Override
    public Page<ReleaseView> selectProcurementPlanInfoAPP(ProcurementPlanInfo procurementPlanInfo) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (procurementPlanInfo.getCurrentPage() - 1)
                * procurementPlanInfo.getPageSize());
        pageView.put("commonId", procurementPlanInfo.getCommonId());
        pageView.put("pageSize", procurementPlanInfo.getPageSize());
        pageView.put("currentPage", procurementPlanInfo.getCurrentPage());
        // 查询计划总数
//        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<ReleaseView> dataList = baseMapper.getListByConditionAPP(pageView);
        Page<ReleaseView> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
//        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(procurementPlanInfo.getCurrentPage());
        page.setSize(procurementPlanInfo.getPageSize());
        return page;
    }
}
