package com.mosty.sjzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.mosty.base.model.dto.sjzx.TbSjRzDto;
import com.mosty.base.model.dto.sjzx.TbSjRzInsertDto;
import com.mosty.base.model.entity.sjzx.TbSj;
import com.mosty.base.model.entity.sjzx.TbSjRz;
import com.mosty.base.model.query.jcgl.TbSjRzQuery;
import com.mosty.base.utils.DateUtils;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.sjzx.mapper.TbSjRzMapper;
import com.mosty.sjzx.service.TbSjRzService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class TbSjRzServiceImpl extends ServiceImpl<TbSjRzMapper, TbSjRz>
        implements TbSjRzService {

    @Override
    public void saveRz(TbSjRzInsertDto dto) {
        UserInfo user = UserInfoManager.get();
        TbSjRz rz = new TbSjRz();
        BeanUtils.copyProperties(dto, rz);
        Date time = new Date();
        rz.setCzsj(time);
        rz.setRzms(user.getDeptName() + "(" + user.getUserName() + ")于" +
                DateUtils.getQueryDateString(time, "02") + dto.getRzms());
        rz.setCzryId(String.valueOf(user.getUserId()));
        rz.setCzrySfzh(user.getIdEntityCard());
        rz.setCzryXm(user.getUserName());
        rz.setXtSjly("1");
        rz.setSsbm(user.getDeptName());
        rz.setSsbmid(String.valueOf(user.getDeptId()));
        rz.setSsbmdm(user.getDeptCode());
        rz.setSsxgaj(user.getFxjDeptName());
        rz.setSsxgajid(String.valueOf(user.getFxjDeptId()));
        rz.setSsxgajdm(user.getFxjDeptCode());
        rz.setSssgaj(user.getDszDeptName());
        rz.setSssgajid(String.valueOf(user.getDszDeptId()));
        rz.setSssgajdm(user.getDszDeptCode());
        this.baseMapper.insert(rz);
    }

    @Override
    public IPage<TbSjRzDto> getPageList(TbSjRzQuery dto) {
        if (StringUtils.isBlank(dto.getSjId()))
            throw new BusinessException("sjId不能为空！");
        IPage<TbSjRz> page = this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbSjRz>()
                        .eq(TbSjRz::getSjId, dto.getSjId())
                        .like(StringUtils.isNotBlank(dto.getRzms()), TbSjRz::getRzms, dto.getRzms())
                        .orderByDesc(TbSjRz::getCzsj)
        );
        IPage<TbSjRzDto> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbSjRzDto> resList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbSjRzDto res = new TbSjRzDto();
                BeanUtils.copyProperties(item, res);
                resList.add(res);
            });
        }
        resPage.setTotal(page.getTotal());
        resPage.setRecords(resList);
        return resPage;
    }
}
