package com.hzya.frame.sysnew.messageManage.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.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.messageManageDetail.dao.ISysMessageManageDetailDao;
import com.hzya.frame.sysnew.messageManageDetail.entity.SysMessageManageDetailEntity;
import com.hzya.frame.sysnew.messageManage.dao.ISysMessageManageDao;
import com.hzya.frame.sysnew.messageManage.entity.SysMessageManageEntity;
import com.hzya.frame.sysnew.messageManage.entity.SysMessageManagePageVo;
import com.hzya.frame.sysnew.messageManage.service.ISysMessageManageService;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 消息管理主表(SysMessageManage)表服务实现类
 *
 * @author makejava
 * @since 2023-08-28 16:45:46
 */
@Service(value="sysMessageManageService")
public class SysMessageManageServiceImpl extends BaseService<SysMessageManageEntity, String> implements ISysMessageManageService {

    private ISysMessageManageDao sysMessageManageDao;

    @Autowired
    public void setSysMessageManageDao(ISysMessageManageDao dao) {
        this.sysMessageManageDao = dao;
        this.dao = dao;
    }

    @Resource
    private ISysMessageManageDetailDao sysMessageManageDetailDao; //明细表

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 分页查询，转换显示字段，以及返回VO对象
     * @Date 9:45 上午 2023/8/29
     **/
    @Override
    public JsonResultEntity queryPagedVo(JSONObject object) {
        SysMessageManageEntity entity = getData("jsonStr", object, SysMessageManageEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<SysMessageManagePageVo> sysMessageManageEntities = sysMessageManageDao.queryListByPageVo(entity);
        PageInfo pageInfo = new PageInfo(sysMessageManageEntities);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 根据id查询对象
     * @Date 9:45 上午 2023/8/29
     **/
    @Override
    public JsonResultEntity queryEntity(JSONObject object) {
        SysMessageManageEntity entity = getData("jsonStr", object, SysMessageManageEntity.class);
        //判断主键
        if (entity == null || entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先传递请求主键");
        }
        entity = sysMessageManageDao.queryOne(entity);
        if (entity != null && entity.getId() != null) {
            //查询明细数据
            SysMessageManageDetailEntity sysMessageManageDetailEntity = new SysMessageManageDetailEntity();
            sysMessageManageDetailEntity.setFormmainId(entity.getId());
            sysMessageManageDetailEntity.setSts("Y");
            List<SysMessageManageDetailEntity> detailList = sysMessageManageDetailDao.queryBase(sysMessageManageDetailEntity);
            entity.setDetailList(detailList);
            return BaseResult.getSuccessMessageEntity("查询数据成功", entity);
        } else {
            return BaseResult.getFailureMessageEntity("未查询到数据");
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存对象，包括明细表，校验编码是否有重复
     * @Date 9:45 上午 2023/8/29
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject object) {
        SysMessageManageEntity entity = getData("jsonStr", object, SysMessageManageEntity.class);
        //判断编码是否重复
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("请先填写内容");
        }
        if (entity.getCode() == null || "".equals(entity.getCode())) {
            return BaseResult.getFailureMessageEntity("请先输入编码");
        }
        if (checkCode(entity.getCode())) {
            return BaseResult.getFailureMessageEntity("编码已存在");
        }
        //判断明细编码是否重复
        List<SysMessageManageDetailEntity> detailList = entity.getDetailList();
        if(detailList != null && detailList.size() > 1){
            Set<String> codeSet = new HashSet<>();
            for (int i = 0; i < detailList.size(); i++) {
                codeSet.add(detailList.get(i).getReceiveCode());
            }
            if(codeSet.size() != detailList.size()){
                return BaseResult.getFailureMessageEntity("消息管理明细的编码重复");
            }
        }
        //保存主表和明细表
        setCreate(entity);
        sysMessageManageDao.save(entity);
        for (int i = 0; i < detailList.size(); i++) {
            setDetailCreate(detailList.get(i));
            detailList.get(i).setFormmainId(entity.getId());
            sysMessageManageDetailDao.save(detailList.get(i));
        }
        return BaseResult.getSuccessMessageEntity("保存成功");
    }

    private void setCreate(SysMessageManageEntity entity) {
        entity.setId(UUIDUtils.getUUID());
        entity.setSts("Y");
        entity.setCreate_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setCreate_time(new Date());
        entity.setModify_time(new Date());
    }
    private void setDetailCreate(SysMessageManageDetailEntity entity) {
        entity.setId(UUIDUtils.getUUID());
        entity.setSts("Y");
        entity.setCreate_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setCreate_time(new Date());
        entity.setModify_time(new Date());
    }
    /**
     * @param code
     * @return boolean
     * @Author lvleigang
     * @Description 校验编码是否重复 存在true 不存在false
     * @Date 10:31 上午 2023/8/29
     **/
    private boolean checkCode(String code) {
        SysMessageManageEntity sysMessageManageEntity = new SysMessageManageEntity();
        sysMessageManageEntity.setCode(code);
        sysMessageManageEntity.setSts("Y");
        int count = sysMessageManageDao.getCount(sysMessageManageEntity);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改对象，包括明细表，校验编码是否有重复
     * @Date 9:45 上午 2023/8/29
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject object) {
        SysMessageManageEntity entity = getData("jsonStr", object, SysMessageManageEntity.class);
        //判断编码是否重复
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("请先填写内容");
        }

        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getCode() == null || "".equals(entity.getCode())) {
            return BaseResult.getFailureMessageEntity("请先输入编码");
        }
        SysMessageManageEntity checkEntity = new SysMessageManageEntity();
        checkEntity.setSts("Y");
        checkEntity.setId(entity.getId());
        checkEntity.setCode(entity.getCode());
        int checkCount = sysMessageManageDao.entity_count_not_id(checkEntity);
        if(checkCount > 0){
            return BaseResult.getFailureMessageEntity("编码已存在");
        }
        //判断明细编码是否重复
        List<SysMessageManageDetailEntity> detailList = entity.getDetailList();
        if(detailList != null && detailList.size() > 1){
            Set<String> codeSet = new HashSet<>();
            for (int i = 0; i < detailList.size(); i++) {
                codeSet.add(detailList.get(i).getReceiveCode());
            }
            if(codeSet.size() != detailList.size()){
                return BaseResult.getFailureMessageEntity("消息管理明细的编码重复");
            }
        }
        //修改主表和明细表
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        entity.setSts("Y");
        sysMessageManageDao.update(entity);

        SysMessageManageDetailEntity detailEntity = new SysMessageManageDetailEntity();
        detailEntity.setSts("Y");
        detailEntity.setFormmainId(entity.getId());
        detailEntity.setModify_user_id(StpUtil.getLoginIdAsString());
        detailEntity.setModify_time(new Date());
        sysMessageManageDetailDao.logicRemoveMultiCondition(detailEntity);

        for (int i = 0; i < detailList.size(); i++) {
            setDetailCreate(detailList.get(i));
            detailList.get(i).setFormmainId(entity.getId());
            sysMessageManageDetailDao.save(detailList.get(i));
        }
        return BaseResult.getSuccessMessageEntity("修改成功");

    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除对象，包括明细表
     * @Date 9:45 上午 2023/8/29
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject object) {
        SysMessageManageEntity entity = getData("jsonStr", object, SysMessageManageEntity.class);
        //判断编码是否重复
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("请先填写内容");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("请先选择数据");
        }
        //删除主表和明细表
        entity.setModify_user_id(StpUtil.getLoginIdAsString());
        entity.setModify_time(new Date());
        entity.setSts("Y");
        sysMessageManageDao.logicRemoveMultiCondition(entity);

        SysMessageManageDetailEntity detailEntity = new SysMessageManageDetailEntity();
        detailEntity.setSts("Y");
        detailEntity.setFormmainId(entity.getId());
        detailEntity.setModify_user_id(StpUtil.getLoginIdAsString());
        detailEntity.setModify_time(new Date());
        sysMessageManageDetailDao.logicRemoveMultiCondition(detailEntity);

        return BaseResult.getSuccessMessageEntity("删除成功");
    }
}
