package com.usky.fire.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemLawSituation;
import com.usky.fire.mapper.DemLawSituationMapper;
import com.usky.fire.service.DemLawSituationService;
import com.usky.fire.service.vo.DemLawSituationExportVO;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政处罚情况表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-27
 */
@Service
public class DemLawSituationServiceImpl extends AbstractCrudService<DemLawSituationMapper, DemLawSituation> implements DemLawSituationService {

    @Override
    public CommonPage<Object> lawSituationList(String punishedPerson, String legalRepresentative, Integer id, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<DemLawSituation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemLawSituation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemLawSituation::getId, DemLawSituation::getPunishedPerson, DemLawSituation::getCounterpartType,
                DemLawSituation::getLegalRepresentative, DemLawSituation::getDocumentType, DemLawSituation::getDocumentNumber,
                DemLawSituation::getPunishmentDecisionNumber, DemLawSituation::getPunishmentBasis, DemLawSituation::getPunishmentType,
                DemLawSituation::getPunishmentContent, DemLawSituation::getPenaltyAmount, DemLawSituation::getPunishmentDecisionTime,
                DemLawSituation::getPunishmentValidityDate, DemLawSituation::getPublicityDeadline, DemLawSituation::getPunishmentAuthority)
                .eq(DemLawSituation::getEnable, 0)
                .eq(id != null & id != 0, DemLawSituation::getId, id)
                .like(StringUtils.isNotBlank(punishedPerson), DemLawSituation::getPunishedPerson, punishedPerson)
                .like(StringUtils.isNotBlank(legalRepresentative), DemLawSituation::getLegalRepresentative, legalRepresentative)
                .orderByDesc(DemLawSituation::getId);
        page = this.page(page, queryWrapper);
        for (int i = 0; i < page.getRecords().size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", page.getRecords().get(i).getId());
            map.put("punishedPerson", page.getRecords().get(i).getPunishedPerson());
            map.put("counterpartType", page.getRecords().get(i).getCounterpartType());
            map.put("legalRepresentative", page.getRecords().get(i).getLegalRepresentative());
            map.put("documentType", page.getRecords().get(i).getDocumentType());
            map.put("documentNumber", page.getRecords().get(i).getDocumentNumber());
            map.put("punishmentDecisionNumber", page.getRecords().get(i).getPunishmentDecisionNumber());
            map.put("punishmentBasis", page.getRecords().get(i).getPunishmentBasis());
            map.put("punishmentType", page.getRecords().get(i).getPunishmentType());
            map.put("punishmentContent", page.getRecords().get(i).getPunishmentContent());
            map.put("penaltyAmount", page.getRecords().get(i).getPenaltyAmount());
            map.put("punishmentDecisionTime", page.getRecords().get(i).getPunishmentDecisionTime());
            map.put("punishmentValidityDate", page.getRecords().get(i).getPunishmentValidityDate());
            map.put("publicityDeadline", page.getRecords().get(i).getPublicityDeadline());
            map.put("punishmentAuthority", page.getRecords().get(i).getPunishmentAuthority());
            list.add(map);
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemLawSituationExportVO> lawSituationListExport(String punishedPerson, String legalRepresentative, Integer id){
        List<DemLawSituationExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemLawSituation> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemLawSituation::getId, DemLawSituation::getPunishedPerson, DemLawSituation::getCounterpartType,
                DemLawSituation::getLegalRepresentative, DemLawSituation::getDocumentType, DemLawSituation::getDocumentNumber,
                DemLawSituation::getPunishmentDecisionNumber, DemLawSituation::getPunishmentBasis, DemLawSituation::getPunishmentType,
                DemLawSituation::getPunishmentContent, DemLawSituation::getPenaltyAmount, DemLawSituation::getPunishmentDecisionTime,
                DemLawSituation::getPunishmentValidityDate, DemLawSituation::getPublicityDeadline, DemLawSituation::getPunishmentAuthority)
                .eq(DemLawSituation::getEnable, 0)
                .eq(id != null & id != 0, DemLawSituation::getId, id)
                .like(StringUtils.isNotBlank(punishedPerson), DemLawSituation::getPunishedPerson, punishedPerson)
                .like(StringUtils.isNotBlank(legalRepresentative), DemLawSituation::getLegalRepresentative, legalRepresentative)
                .orderByDesc(DemLawSituation::getId);
        List<DemLawSituation> list1 = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list1)){
            for (int i = 0; i < list1.size(); i++) {
                DemLawSituationExportVO demLawSituationExportVO = new DemLawSituationExportVO();
                demLawSituationExportVO.setPunishedPerson(list1.get(i).getPunishedPerson());
                demLawSituationExportVO.setCounterpartType(list1.get(i).getCounterpartType());
                demLawSituationExportVO.setLegalRepresentative(list1.get(i).getLegalRepresentative());
                demLawSituationExportVO.setPunishmentDecisionNumber(list1.get(i).getPunishmentDecisionNumber());
                demLawSituationExportVO.setPunishmentBasis(list1.get(i).getPunishmentBasis());
                demLawSituationExportVO.setPunishmentType(list1.get(i).getPunishmentType());
                demLawSituationExportVO.setPunishmentContent(list1.get(i).getPunishmentContent());
                demLawSituationExportVO.setPenaltyAmount(list1.get(i).getPenaltyAmount());
                if((list1.get(i).getPunishmentDecisionTime()) != null){
                    demLawSituationExportVO.setPunishmentDecisionTime((list1.get(i).getPunishmentDecisionTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }else{
                    demLawSituationExportVO.setPunishmentDecisionTime(null);
                }
                if((list1.get(i).getPunishmentValidityDate()) != null){
                    demLawSituationExportVO.setPunishmentValidityDate((list1.get(i).getPunishmentValidityDate()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }else{
                    demLawSituationExportVO.setPunishmentValidityDate(null);
                }
                if((list1.get(i).getPublicityDeadline()) != null){
                    demLawSituationExportVO.setPublicityDeadline((list1.get(i).getPublicityDeadline()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }else{
                    demLawSituationExportVO.setPublicityDeadline(null);
                }
                demLawSituationExportVO.setPunishmentAuthority(list1.get(i).getPunishmentAuthority());
                demLawSituationExportVO.setDocumentType(list1.get(i).getDocumentType());
                demLawSituationExportVO.setDocumentNumber(list1.get(i).getDocumentNumber());

                list.add(demLawSituationExportVO);
            }
        }

        return list;
    }

    @Override
    public void updateLawSituation(DemLawSituation demLawSituation) {
        this.updateById(demLawSituation);
    }

    @Override
    public void delLawSituation(Integer id) {
        DemLawSituation demLawSituation = new DemLawSituation();
        demLawSituation.setId(id);
        demLawSituation.setEnable(1);
        this.updateById(demLawSituation);
    }
}
