package com.yifu.cloud.plus.v1.yifu.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.salary.entity.TSpecialDeducationSum;
import com.yifu.cloud.plus.v1.yifu.salary.mapper.TSpecialDeducationSumMapper;
import com.yifu.cloud.plus.v1.yifu.salary.service.TSpecialDeducationSumService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author fxj
 * @Description 专项扣除
 * @Date 13:19 2022/8/6
 * @Param
 * @return
**/
@Service("tSpecialDeducationSumService")
public class TSpecialDeducationSumServiceImpl extends ServiceImpl<TSpecialDeducationSumMapper, TSpecialDeducationSum> implements TSpecialDeducationSumService {

     /**
      * 简单分页查询
      *
      * @param tSpecialDeducationSum
      * @return
      */
     @Override
     public IPage<TSpecialDeducationSum> getTSpecialDeducationSumPage(Page<TSpecialDeducationSum> page, TSpecialDeducationSum tSpecialDeducationSum) {
          return baseMapper.getTSpecialDeducationSumPage(page, tSpecialDeducationSum);
     }

     /**
      * @param tSpecialDeducationSum
      * @Description: 查找专项信息
      * @Author: hgw
      * @Date: 2019/9/30 16:19
      * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.yifu.cloud.v1.hrms.api.entity.TSpecialDeducationSum>
      **/
     @Override
     public List<TSpecialDeducationSum> getTSpecialDeducationSumList(TSpecialDeducationSum tSpecialDeducationSum) {
          return baseMapper.getTSpecialDeducationSumList(tSpecialDeducationSum);
     }

     /**
      * @Description: 查找专项信息
      * @Author: hgw
      * @Date: 2019/9/30 16:19
      * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.yifu.cloud.v1.hrms.api.entity.TSpecialDeducationSum>
      **/
     @Override
     public TSpecialDeducationSum getTSpecialDeducationSum(String yearMonth, String idNumber, String invoiceTitle) {
          return baseMapper.getTSpecialDeducationSum(yearMonth, idNumber, invoiceTitle);
     }

     /**
      * @param yearMonth
      * @param idCardList
      * @Description: 工资查询所需的全部list，组装Map来使用
      * @Author: hgw
      * @Date: 2022/1/26 16:45
      * @return: java.util.Map<java.lang.String, com.yifu.cloud.v1.hrms.api.entity.TSpecialDeducationSum>
      **/
     @Override
     public Map<String, TSpecialDeducationSum> getTSpecialDeducationSumMap(String yearMonth, List<String> idCardList) {
          Map<String, TSpecialDeducationSum> sdsMap = new HashMap<>();
          if (Common.isNotNull(yearMonth) && idCardList != null && !idCardList.isEmpty()) {
               List<TSpecialDeducationSum> sdsList = baseMapper.getListByIdCardList(yearMonth, idCardList);
               if (sdsList != null && !sdsList.isEmpty()) {
                    for (TSpecialDeducationSum sds : sdsList) {
                         sdsMap.put(sds.getIdNumber() + CommonConstants.DOWN_LINE_STRING + sds.getDeclareTitle(), sds);
                    }
               }
          }
          return sdsMap;
     }

     /**
      * @param yearMonth 年月
      * @param invoiceTitle 申报单位
      * @Description: 删除专项扣除
      * @Author: hgw
      * @Date: 2020/4/1 15:11
      * @return: int
      **/
     @Override
     public int deleteByUnitAndMonth(String yearMonth, String invoiceTitle) {
          return baseMapper.deleteByUnitAndMonth(yearMonth, invoiceTitle);
     }

     /**
      * @param sdsList
      * @Description: 保存专项
      * @Author: hgw
      * @Date: 2019/10/14 14:48
      * @return: com.yifu.cloud.v1.common.core.util.R
      **/
     @Override
     public R uploadSds(List<TSpecialDeducationSum> sdsList) {
          if (sdsList != null && sdsList.size() > CommonConstants.ZERO_INT) {
               TSpecialDeducationSum sds = null;
               int rowNum = sdsList.size();
               String nowYearMonth = DateUtil.getCurrentDateString("yyyyMM");
               String yearMonth = DateUtil.getYearMonth(sdsList.get(0).getCreateStart());
               if (Common.isNotNull(yearMonth)) {
                    if (!yearMonth.equals(nowYearMonth)) {
                         return R.failed("所得期间-不是当前年月:" + nowYearMonth + "，不允许导入");
                    }
               } else {
                    return R.failed("请检查‘所得期间起’单元格格式是文本格式，且满足日期格式yyyy-MM-dd或yyyyMMdd");
               }
               String declareTitle = sdsList.get(0).getDeclareTitle();
               if (Common.isEmpty(declareTitle)) {
                    return R.failed("申报单位不能为空");
               }

               TSpecialDeducationSum sdsCheck = new TSpecialDeducationSum();
               sdsCheck.setCreateMonth(yearMonth);
               sdsCheck.setDeclareTitle(declareTitle);
               QueryWrapper<TSpecialDeducationSum> queryWrapperSc = new QueryWrapper<>();
               queryWrapperSc.setEntity(sdsCheck);
               List<TSpecialDeducationSum> scList = this.list(queryWrapperSc);
               if (scList != null && scList.size() > CommonConstants.ZERO_INT) {
                    return R.failed("当前月同一申报单位已存在数据，请先删除");
               }
               YifuUser user = SecurityUtils.getUser();
               if (null == user || null == user.getId()) {
                    return R.failed("新建失败-获取登录人信息为空");
               }
               LocalDateTime nowDate = LocalDateTime.now();
               for (TSpecialDeducationSum s : sdsList) {
                    s.setCreateMonth(yearMonth);
                    s.setCreateTime(nowDate);
                    s.setCreateBy(String.valueOf(user.getId()));
               }
               return new R<>(this.saveBatch(sdsList));
          }
          return R.failed("数据不可为空");
     }

}
