package com.mortals.xhx.module.matter.service.impl;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.MatterSourceEnum;
import com.mortals.xhx.common.key.ParamKey;
import com.mortals.xhx.common.utils.StringUtils;
import com.mortals.xhx.module.matter.model.MatterDatumEntity;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import com.mortals.xhx.module.sheet.dao.SheetMatterDao;
import com.mortals.xhx.module.sheet.model.SheetMatterEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.matter.dao.MatterDao;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.service.MatterService;

import java.util.*;

/**
* MatterService
* 事项申请材料 service实现
*
* @author zxfei
* @date 2022-09-27
*/
@Service("matterService")
public class MatterServiceImpl extends AbstractCRUDServiceImpl<MatterDao, MatterEntity, Long> implements MatterService {

    private static int RECOMMEND_COUNT = 5;
    @Autowired
    private ParamService paramService;
    @Autowired
    private SheetMatterDao sheetMatterDao;
    @Autowired
    private MatterDatumService matterDatumService;

    @Override
    protected MatterEntity findBefore(MatterEntity params, PageInfo pageInfo, Context context) throws AppException {
        if(StringUtils.isNotEmpty(params.getMatterName())){
            params.setMatterName("%".concat(params.getMatterName()).concat("%"));
        }
        return params;
    }

    @Override
    protected void saveBefore(MatterEntity entity, Context context) throws AppException {
        entity.setTotal(0);
        entity.setSort(0);
        entity.setIsRecommend(0);
        entity.setSource(MatterSourceEnum.MANUAL.getValue());
        this.validData(entity, context);
    }

    @Override
    protected void updateBefore(MatterEntity entity, Context context) throws AppException {
        entity.setIsRecommend(null);
        this.validData(entity, context);
    }

    @Override
    protected void removeBefore(Long[] ids, Context context) throws AppException {
        List<MatterEntity> queryList = this.get(ids,context);
        if(CollectionUtils.isNotEmpty(queryList)){
            int recommendCount = paramService.getParamIntValue(ParamKey.MATTER_RECOMMEND_COUNT);
            for(MatterEntity matterEntity:queryList){
                if(matterEntity.getIsRecommend()==1){
                    recommendCount--;
                }
            }
            paramService.setValueByKey(ParamKey.MATTER_RECOMMEND_COUNT,String.valueOf(recommendCount));
        }
    }

    @Override
    protected void findAfter(MatterEntity entity, PageInfo pageInfo, Context context, List<MatterEntity> list) throws AppException {
        if(CollectionUtils.isNotEmpty(list)){
            for(MatterEntity matterEntity:list){
                MatterDatumEntity query = new MatterDatumEntity();
                query.setMatterId(matterEntity.getId());
                int datumCount = matterDatumService.count(query,context);
                matterEntity.setDatumCount(datumCount);
            }
        }
    }

    @Override
    public void createMatterbBySheetMatter(Long[] sheetMatterIds) {
        List<SheetMatterEntity> sheetMatterEntityList = sheetMatterDao.get(sheetMatterIds);
        if(CollectionUtils.isNotEmpty(sheetMatterEntityList)){
            Date now = new Date();
            List<MatterEntity> list = new ArrayList<>();
            for(SheetMatterEntity sheetMatterEntity:sheetMatterEntityList){
                MatterEntity query = new MatterEntity();
                query.setSiteId(sheetMatterEntity.getSiteId());
                query.setTid(sheetMatterEntity.getTid());
                query.setTcode(sheetMatterEntity.getTcode());
                List<MatterEntity> queryList = this.find(query);
                if(CollectionUtils.isNotEmpty(queryList)){
                    continue;
                }
                MatterEntity matterEntity = new MatterEntity();
                matterEntity.setSiteId(sheetMatterEntity.getSiteId());
                matterEntity.setTid(sheetMatterEntity.getTid());
                matterEntity.setTcode(sheetMatterEntity.getTcode());
                matterEntity.setTname(sheetMatterEntity.getTname());
                matterEntity.setMatterName(sheetMatterEntity.getMatterName());
                matterEntity.setEnglishName(sheetMatterEntity.getEnglishName());
                matterEntity.setMatterNo(sheetMatterEntity.getMatterNo());
                matterEntity.setMatterFullName(sheetMatterEntity.getMatterName());
                matterEntity.setTotal(0);
                matterEntity.setSort(0);
                matterEntity.setIsRecommend(0);
                matterEntity.setSource(MatterSourceEnum.AUTO.getValue());
                matterEntity.setCreateUserId(1l);
                matterEntity.setCreateTime(now);
                list.add(matterEntity);
            }
            this.dao.insertBatch(list);
        }

    }

    @Override
    public void recommend(Long id) {
        MatterEntity matterEntity = this.dao.get(id);
        if(matterEntity==null){
            throw new AppException("数据已更改,请重试");
        }
        int recommendCount = paramService.getParamIntValue(ParamKey.MATTER_RECOMMEND_COUNT);
        Map<String,Object> data = new HashMap<>();
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",id);
        if(matterEntity.getIsRecommend()==0){
            if(recommendCount>=RECOMMEND_COUNT){
                throw new AppException("超过推荐个数");
            }
            recommendCount++;
            data.put("isRecommend",1);
        }else {
            recommendCount--;
            data.put("isRecommend",0);
        }
        this.dao.update(data,condition);
        paramService.setValueByKey(ParamKey.MATTER_RECOMMEND_COUNT,String.valueOf(recommendCount));
    }
}