package com.hzya.frame.sys.proof.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sys.proof.entity.JournalingDto;
import com.hzya.frame.sys.proof.entity.PullVo;
import com.hzya.frame.sys.proofDetail.dao.IProofDetailDao;
import com.hzya.frame.sys.proofDetail.entity.ProofDetailEntity;
import com.hzya.frame.sys.voucher.dao.IVoucherDao;
import com.hzya.frame.sys.voucher.entity.VoucherEntity;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzya.frame.sys.proof.entity.ProofEntity;
import com.hzya.frame.sys.proof.service.IProofService;
import com.hzya.frame.sys.proof.dao.IProofDao;
import com.hzya.frame.basedao.service.impl.BaseService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description: 凭证  service
 * @tableName: sys_proof
 * @entityName: ProofEntity
 * @author: gjh
 * @history: 1.0
 */
@Service(value = "sys_proofService")
public class ProofServiceImpl extends BaseService<ProofEntity, String> implements IProofService {

    protected IProofDao proofDao;
    @Resource
	protected IProofDetailDao proofdetailDao;
    @Resource
    protected IVoucherDao voucherDao;
    @Autowired
    public void setProofDao(IProofDao dao) {
        this.proofDao = dao;
        this.dao = dao;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询凭证
     * @Date 3:54 下午 2023/8/2
     **/
    @Override
    public JsonResultEntity queryProofPage(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
        //判断单据状态
        if (!checkInt(entity.getPageNum())) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        if (!checkInt(entity.getPageSize())) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<ProofEntity> proofEntities = proofDao.queryByLike(entity);
        PageInfo pageInfo = new PageInfo(proofEntities);

        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取凭证
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity getProof(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
		//判断id
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
		entity = proofDao.get(entity.getId());
        if(entity.getId() != null){
        	//查询分录
			ProofDetailEntity proofDetailEntity = new ProofDetailEntity();
			proofDetailEntity.setFormmain_id(entity.getId());
			proofDetailEntity.setSts("Y");
			List<ProofDetailEntity> proofDetailEntityList = proofdetailDao.query(proofDetailEntity);
			entity.setProofDetails(proofDetailEntityList);
		}
		return BaseResult.getSuccessMessageEntity("查询凭证成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 推送凭证
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity pushProof(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
        PageHelper.startPage(1, 100);
        List<ProofEntity> proofEntityList = proofDao.queryPushProof(entity);
        if(proofEntityList != null && proofEntityList.size() > 0){
            for (int i = 0; i < proofEntityList.size(); i++) {
                //查询分录
                ProofDetailEntity proofDetailEntity = new ProofDetailEntity();
                proofDetailEntity.setFormmain_id(proofEntityList.get(i).getId());
                proofDetailEntity.setSts("Y");
                List<ProofDetailEntity> proofDetailEntityList = proofdetailDao.query(proofDetailEntity);
                proofEntityList.get(i).setProofDetails(proofDetailEntityList);
                //修改状态
                ProofEntity changeentity = new ProofEntity();
                changeentity.setId(proofEntityList.get(i).getId());
                changeentity.setCertificate_status("2");
                changeentity.setModify_user_id(StpUtil.getLoginIdAsString());
                changeentity.setModify_time(new Date());
                proofDao.update(changeentity);
            }
        }
		return BaseResult.getSuccessMessageEntity("查询凭证成功", proofEntityList);
    }
    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改凭证
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity changeProof(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
		//判断id
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        ProofEntity checkEntity = proofDao.get(entity.getId());
        if(checkEntity == null ){
            return BaseResult.getFailureMessageEntity("凭证不存在");
        }
        if(checkEntity.getCertificate_status() != null && !"5".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证不可修改");
        }
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        proofDao.update(entity);

        //修改明细 先删除再添加
        ProofDetailEntity deleteProofDetail = new ProofDetailEntity();
        deleteProofDetail.setFormmain_id(entity.getId());
        deleteProofDetail.setModify_user_id(StpUtil.getLoginIdAsString());
        deleteProofDetail.setModify_time(new Date());
        proofdetailDao.logicRemoveMultiCondition(deleteProofDetail);

        if(entity.getProofDetails() != null && entity.getProofDetails().size() > 0){
            for (int i = 0; i < entity.getProofDetails().size(); i++) {
                ProofDetailEntity proofDetailEntity = entity.getProofDetails().get(i);
                proofDetailEntity.setId(UUIDUtils.getUUID());
                proofDetailEntity.setSts("Y");
                proofDetailEntity.setCreate_user_id(StpUtil.getLoginIdAsString());
                proofDetailEntity.setModify_user_id(StpUtil.getLoginIdAsString());
                proofDetailEntity.setCreate_time(new Date());
                proofDetailEntity.setModify_time(new Date());
                proofDetailEntity.setFormmain_id(entity.getId());
                proofDetailEntity.setSorts(i+1L);
                proofdetailDao.save(proofDetailEntity);
            }
        }
		return BaseResult.getSuccessMessageEntity("修改凭证成功");
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 三方撤销凭证
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity tripartiteRevocation(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
		//判断id
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        ProofEntity checkEntity = proofDao.get(entity.getId());
        if(checkEntity == null ){
            return BaseResult.getFailureMessageEntity("凭证不存在");
        }
        if(checkEntity.getCertificate_status() != null && "5".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证已经推送撤销");
        }
        if(checkEntity.getCertificate_status() != null && "1".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证未推送不需要撤销");
        }
        if(checkEntity.getCertificate_status() != null && "4".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证推送失败不需要撤销");
        }
        if(checkEntity.getCertificate_status() != null && "6".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证已经删除不需要撤销");
        }
        if(doTripartiteRevocation(entity)){
            //成功
            //修改状态
            ProofEntity proofEntity = new ProofEntity();
            proofEntity.setId(entity.getId());
            proofEntity.setModify_user_id(StpUtil.getLoginIdAsString());
            proofEntity.setModify_time(new Date());
            proofEntity.setCertificate_status("5");
            proofDao.update(proofEntity);
            return BaseResult.getFailureMessageEntity("推送第三方撤销凭证成功");
        }else {
            //失败
            //提示前端
            return BaseResult.getFailureMessageEntity("推送第三方撤销凭证失败");
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除凭证
     * @Date 1:33 下午 2023/7/12
     **/
    @Override
    public JsonResultEntity deleteProof(JSONObject object) {
        ProofEntity entity = getData("jsonStr", object, ProofEntity.class);
		//判断id
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        ProofEntity checkEntity = proofDao.get(entity.getId());
        if(checkEntity == null ){
            return BaseResult.getFailureMessageEntity("凭证不存在");
        }
        if(checkEntity.getCertificate_status() != null && "2".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证已推送不允许删除");
        }
        if(checkEntity.getCertificate_status() != null && "3".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证已推送不允许删除");
        }
        if(checkEntity.getCertificate_status() != null && "6".equals(checkEntity.getCertificate_status()) ){
            return BaseResult.getFailureMessageEntity("当前凭证已删除不允许再次删除");
        }
        //成功
        //修改状态
        ProofEntity proofEntity = new ProofEntity();
        proofEntity.setId(entity.getId());
        proofEntity.setModify_user_id(StpUtil.getLoginIdAsString());
        proofEntity.setModify_time(new Date());
        proofEntity.setCertificate_status("6");
        proofEntity.setSts("N");
        proofDao.update(proofEntity);

        //修改原数据
        VoucherEntity voucherEntity = new VoucherEntity();
        voucherEntity.setProof_id(entity.getId());
        voucherEntity.setReceipt_status("1");
        voucherEntity.setModify_user_id(StpUtil.getLoginIdAsString());
        voucherEntity.setModify_time(new Date());
        voucherDao.VoucherEntity_update_By_Proof_id(voucherEntity);

        return BaseResult.getFailureMessageEntity("删除凭证成功");
    }

    /**
     * @Author lvleigang
     * @Description  推送第三方删除 todo
     * @Date 4:39 下午 2023/8/2
     * @param entity
     * @return boolean
     **/
    private boolean doTripartiteRevocation(ProofEntity entity) {

        return true;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取科目下拉的数据
     * @Date 3:54 下午 2023/8/2
     **/
    @Override
    public JsonResultEntity getJournalingPage(JSONObject object) {
        JournalingDto entity = getData("jsonStr", object, JournalingDto.class);
        //判断单据状态
        if (!checkInt(entity.getPageNum())) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        if (!checkInt(entity.getPageSize())) {
            return BaseResult.getFailureMessageEntity("请先传递分页参数");
        }
        if (!checkStr(entity.getJournalingType())) {
            return BaseResult.getFailureMessageEntity("请先传递查询类型");
        }
        PullVo pullVo = new PullVo();
        pullVo.setRightNames(entity.getNames());
        if("project".equals(entity.getJournalingType())){//项目
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.queryProject(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("supplier".equals(entity.getJournalingType())){//供应商
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.querySupplier(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("client".equals(entity.getJournalingType())){//客户
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.queryClient(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("salesman".equals(entity.getJournalingType())){//业务员
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.querySalesman(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("section".equals(entity.getJournalingType())){//部门
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.querySection(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("sob".equals(entity.getJournalingType())){//帐套
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.querySob(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("receiptConf".equals(entity.getJournalingType())){//单据配置
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.queryReceiptConf(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else if("subject".equals(entity.getJournalingType())){//科目
            PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
            List<PullVo> pullVoList = proofDao.querySubject(pullVo);
            PageInfo pageInfo = new PageInfo(pullVoList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
        }else {
            return BaseResult.getFailureMessageEntity("查询类型错误");
        }
    }
    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取科目下拉的数据
     * @Date 3:54 下午 2023/8/2
     **/
    @Override
    public JsonResultEntity getJournaling(JSONObject object) {
        JournalingDto entity = getData("jsonStr", object, JournalingDto.class);
        //判断单据状态
        if (!checkStr(entity.getJournalingType())) {
            return BaseResult.getFailureMessageEntity("请先传递查询类型");
        }
        if (!checkStr(entity.getCodes())) {
            return BaseResult.getFailureMessageEntity("请先传递查询值");
        }
        PullVo pullVo = new PullVo();
        pullVo.setCodes(entity.getCodes());
        List<PullVo> pullVoList = null;
        if("project".equals(entity.getJournalingType())){//项目
            pullVoList = proofDao.queryProject(pullVo);
        }else if("supplier".equals(entity.getJournalingType())){//供应商
            pullVoList = proofDao.querySupplier(pullVo);
        }else if("client".equals(entity.getJournalingType())){//客户
            pullVoList = proofDao.queryClient(pullVo);
        }else if("salesman".equals(entity.getJournalingType())){//业务员
            pullVoList = proofDao.querySalesman(pullVo);
        }else if("section".equals(entity.getJournalingType())){//
            pullVoList = proofDao.querySection(pullVo);
        }else if("sob".equals(entity.getJournalingType())){//帐套
            pullVoList = proofDao.querySob(pullVo);
        }else if("receiptConf".equals(entity.getJournalingType())){//单据配置
            pullVoList = proofDao.queryReceiptConf(pullVo);
        }else if("subject".equals(entity.getJournalingType())){//科目
            pullVoList = proofDao.querySubject(pullVo);
        }else {
            return BaseResult.getFailureMessageEntity("查询类型错误");
        }
        if(pullVoList != null && pullVoList.size() > 0){
            return BaseResult.getSuccessMessageEntity("查询成功",pullVoList.get(0));
        }else {
            return BaseResult.getSuccessMessageEntity("查询不到数据");
        }
    }
}
