package com.mosty.base.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.mosty.base.manager.mapper.TbJcglKhbmMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglKhbmService;
import com.mosty.base.model.dto.jcgl.TbJcglKhbmEditPzDto;
import com.mosty.base.model.entity.jcgl.TbJcglKhbm;
import com.mosty.base.model.query.jcgl.TbJcglKhbmQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.jcgl.TbJcglKhbmVo;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

@Service
@AllArgsConstructor
public class TbJcglKhbmServiceImpl extends ServiceImpl<TbJcglKhbmMapper, TbJcglKhbm>
        implements TbJcglKhbmService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public void addKhbm(List<String> ssbmidList) {
        UserInfo user = UserInfoManager.get();
        if (!CollectionUtils.isEmpty(ssbmidList)) {
            ssbmidList.forEach(item -> {
                TbJcglKhbm bm = this.baseMapper.selectOne(
                        new LambdaQueryWrapper<TbJcglKhbm>()
                                .eq(TbJcglKhbm::getSsbmid, item)
                );
                if (bm == null) {
                    bm = new TbJcglKhbm();
                    DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(item);
                    bm.setTjrid(String.valueOf(user.getUserId()));
                    bm.setTjrsfzh(user.getIdEntityCard());
                    bm.setTjrxm(user.getUserName());
                    bm.setTjsj(new Date());
                    bm.setSsbm(dept.getDeptname());
                    bm.setSsbmid(dept.getDeptid());
                    bm.setSsbmdm(dept.getDeptcode());
                    bm.setSsxgajdm(dept.getFxjcode());
                    bm.setSsxgajid(dept.getFxjid());
                    bm.setSsxgaj(dept.getFxjname());
                    bm.setSssgaj(dept.getDszname());
                    bm.setSssgajdm(dept.getDszcode());
                    bm.setSssgajid(dept.getDszid());
                    bm.setXtSjly("1");
                    this.baseMapper.insert(bm);
                }
            });
        }
    }

    @Override
    public void delKhbm(List<String> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            this.baseMapper.delKhbm(ids);
        }
    }

    @Override
    public IPage<TbJcglKhbmVo> getPageList(TbJcglKhbmQuery dto) {
//        UserInfo user = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, user);
        dto.setSsbm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbm(), null));

        QueryWrapper<TbJcglKhbm> qw = new QueryWrapper<>();
        IPage<TbJcglKhbm> page = this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                qw.lambda().likeRight(!StringUtils.isEmpty(dto.getSsbm()), TbJcglKhbm::getSsbmdm, dto.getSsbm())
        );
        IPage<TbJcglKhbmVo> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbJcglKhbmVo> resList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbJcglKhbmVo vo = new TbJcglKhbmVo();
                BeanUtils.copyProperties(item, vo);
                resList.add(vo);
            });
        }
        resPage.setRecords(resList);
        resPage.setTotal(page.getTotal());
        return resPage;
    }

    @Override
    public List<String> getAllKhbm() {
//        UserInfo user = UserInfoManager.get();
//        String sql = PermissionsUtil.createSql("", user);
        String ssbmdm = this.tbBaseAdaptRemoteService.getSsbm(null, null);
        return this.baseMapper.getAllKhbm(ssbmdm);
    }

    @Override
    public void editKhbm(TbJcglKhbm dto) {
        TbJcglKhbm bm = this.baseMapper.selectById(dto.getId());
        if (bm == null) throw new BusinessException("考核部门不存在，或已删除");
        this.baseMapper.updateById(dto);
    }
}
