package cn.ey.biz.module.system.service.engagement.business;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ey.biz.module.system.controller.engagement.business.vo.EngagementBusinessCreateReqVO;
import cn.ey.biz.module.system.controller.engagement.business.vo.EngagementBusinessCreateRespVO;
import cn.ey.biz.module.system.controller.engagement.business.vo.EngagementBusinessFieldRecordRespVO;
import cn.ey.biz.module.system.controller.engagement.business.vo.EngagementBusinessLogRespVO;
import cn.ey.biz.module.system.convert.engagement.business.EngagementBusinessFieldConvert;
import cn.ey.biz.module.system.dal.dataobject.engagement.business.SystemEngagementBusinessFieldDO;
import cn.ey.biz.module.system.dal.dataobject.engagement.business.SystemEngagementBusinessFieldLogDO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.dal.mapper.engagement.business.EngagementBusinessFieldLogMapper;
import cn.ey.biz.module.system.dal.mapper.engagement.business.EngagementBusinessFieldMapper;
import cn.ey.biz.module.system.service.user.AdminUserService;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.*;

@Service
public class EngagementBusinessFieldServiceImpl implements EngagementBusinessFieldService {
    @Resource
    private EngagementBusinessFieldMapper engagementBusinessFieldMapper;

    @Resource
    private EngagementBusinessFieldLogMapper engagementBusinessFieldLogMapper;

    @Resource
    private AdminUserService adminUserService;

    @Override
    public Long createBusinessField(EngagementBusinessCreateReqVO reqVO) {
        SystemEngagementBusinessFieldDO businessFieldDO = EngagementBusinessFieldConvert.INSTANCE.voConvertDo(reqVO);
        businessFieldDO.setId(null);
        engagementBusinessFieldMapper.insert(businessFieldDO);
        return businessFieldDO.getId();
    }

    @Override
    public void updateBusinessField(List<EngagementBusinessCreateReqVO> reqVOS) {
        if (CollUtil.isNotEmpty(reqVOS)) {
            for (EngagementBusinessCreateReqVO reqVO : reqVOS) {
                SystemEngagementBusinessFieldDO businessFieldDO = EngagementBusinessFieldConvert.INSTANCE.voConvertDo(reqVO);
                checkStringLength(businessFieldDO);
                SystemEngagementBusinessFieldDO systemEngagementBusinessFieldDO = engagementBusinessFieldMapper.selectById(businessFieldDO);
                SystemEngagementBusinessFieldLogDO systemEngagementBusinessFieldLogDO = new SystemEngagementBusinessFieldLogDO();
                systemEngagementBusinessFieldLogDO.setOriginalId(businessFieldDO.getId());
                systemEngagementBusinessFieldLogDO.setCurrentDataLogs(JSON.toJSONString(reqVO));
                EngagementBusinessCreateRespVO respVO = EngagementBusinessFieldConvert.INSTANCE.doConvertVo(systemEngagementBusinessFieldDO);
                systemEngagementBusinessFieldLogDO.setOriginalDataLogs(JSON.toJSONString(respVO));
                engagementBusinessFieldLogMapper.insert(systemEngagementBusinessFieldLogDO);
                engagementBusinessFieldMapper.updateById(businessFieldDO);
            }
        }
    }

    private void checkStringLength(SystemEngagementBusinessFieldDO businessFieldDO) {
        if (StringUtils.isNotBlank(businessFieldDO.getConsultingCore()) && businessFieldDO.getConsultingCore().length() > 20000) {
            throw exception(CONSULTING_CORE_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getConsultingFso()) && businessFieldDO.getConsultingFso().length() > 20000) {
            throw exception(CONSULTING_FSO_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getAssuranceCore()) && businessFieldDO.getAssuranceCore().length() > 20000) {
            throw exception(ASSURANCE_CORE_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getAssuranceFso()) && businessFieldDO.getAssuranceFso().length() > 20000) {
            throw exception(ASSURANCE_FSO_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getTaxCore()) && businessFieldDO.getTaxCore().length() > 20000) {
            throw exception(TAX_CORE_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getTaxFso()) && businessFieldDO.getTaxFso().length() > 20000) {
            throw exception(TAX_FSO_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getSatCore()) && businessFieldDO.getSatCore().length() > 20000) {
            throw exception(SAT_CORE_TOO_LONG);
        }

        if (StringUtils.isNotBlank(businessFieldDO.getSatFso()) && businessFieldDO.getSatFso().length() > 20000) {
            throw exception(SAT_FSO_TOO_LONG);
        }

    }

    @Override
    public EngagementBusinessCreateRespVO getBusinessFieldDetail(Long id) {
        SystemEngagementBusinessFieldDO systemEngagementBusinessFieldDO = engagementBusinessFieldMapper.selectById(id);
        return EngagementBusinessFieldConvert.INSTANCE.doConvertVo(systemEngagementBusinessFieldDO);
    }

    @Override
    public List<EngagementBusinessCreateRespVO> getBusinessFieldList() {
        List<SystemEngagementBusinessFieldDO> systemEngagementBusinessFieldDOS = engagementBusinessFieldMapper.selectList(new LambdaUpdateWrapper<SystemEngagementBusinessFieldDO>().eq(SystemEngagementBusinessFieldDO::getDeleted, false));
        return EngagementBusinessFieldConvert.INSTANCE.doConvertVoList(systemEngagementBusinessFieldDOS);
    }

    @Override
    public EngagementBusinessFieldRecordRespVO getOperationRecord() {
        SystemEngagementBusinessFieldDO systemEngagementBusinessFieldDO = engagementBusinessFieldMapper.selectOneForMaxTime();
        EngagementBusinessFieldRecordRespVO engagementBusinessFieldRecordRespVO = new EngagementBusinessFieldRecordRespVO();
        if (null != systemEngagementBusinessFieldDO) {
            engagementBusinessFieldRecordRespVO.setUpdater(systemEngagementBusinessFieldDO.getUpdater());
            engagementBusinessFieldRecordRespVO.setUpdateTime(systemEngagementBusinessFieldDO.getUpdateTime());
            if (StringUtils.isNotBlank(engagementBusinessFieldRecordRespVO.getUpdater())) {
                AdminUserDO user = adminUserService.getUser(Long.valueOf(engagementBusinessFieldRecordRespVO.getUpdater()));
                engagementBusinessFieldRecordRespVO.setUpdater(user.getUsername());
            }
        }
        return engagementBusinessFieldRecordRespVO;
    }

    @Override
    public PageResult<EngagementBusinessLogRespVO> getPageLog(PageParam pageParam) {
        LambdaQueryWrapperX<SystemEngagementBusinessFieldLogDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eq(SystemEngagementBusinessFieldLogDO::getDeleted, false).orderByDesc(SystemEngagementBusinessFieldLogDO::getId);
        PageResult<SystemEngagementBusinessFieldLogDO> pageResult = engagementBusinessFieldLogMapper.selectPage(pageParam, queryWrapperX);
        return EngagementBusinessFieldConvert.INSTANCE.pageDoConvertVo(pageResult);
    }
}
