package com.mosty.yjzl.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.entity.yjzl.TbFzycPz;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.yjzl.TbFzycPzVo;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.yjzl.mapper.TbFzycPzMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbFzycPzService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@AllArgsConstructor
public class TbFzycPzServiceImpl extends ServiceImpl<TbFzycPzMapper, TbFzycPz>
        implements TbFzycPzService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public int insert(TbFzycPzVo fzycPzVo) {
        TbFzycPz pz = this.baseMapper.selectOne(new QueryWrapper<TbFzycPz>().eq("ssbmdm", fzycPzVo.getSsbmdm()));
        if (null != pz) {
            throw new BusinessException(fzycPzVo.getSsbm() + "部门已存在犯罪预测配置中！");
        }
        TbFzycPz fzycPz = new TbFzycPz();
        BeanUtil.copyProperties(fzycPzVo, fzycPz);
        fzycPz.setId(UUIDGenerator.getUUID());
        if (StringUtils.isNotBlank(fzycPzVo.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(fzycPzVo.getSsbmdm());
            if (dept != null) {
                fzycPz.setSsbm(dept.getDeptname());
                fzycPz.setSsbmdm(dept.getDeptcode());
                fzycPz.setSsbmid(dept.getDeptid());
                fzycPz.setSsxgaj(dept.getFxjname());
                fzycPz.setSsxgajid(String.valueOf(dept.getFxjid()));
                fzycPz.setSsxgajdm(dept.getFxjcode());
                fzycPz.setSssgaj(dept.getDszname());
                fzycPz.setSssgajid(String.valueOf(dept.getDszid()));
                fzycPz.setSssgajdm(dept.getDszcode());
            }
        }

        return this.baseMapper.insert(fzycPz);
    }

    @Override
    public int updateById(TbFzycPzVo fzycPzVo) {
        TbFzycPz vo = this.baseMapper.selectById(fzycPzVo.getId());

        TbFzycPz fzycPz = new TbFzycPz();
        BeanUtil.copyProperties(fzycPzVo, fzycPz);
        if (StringUtils.isNotEmpty(fzycPzVo.getSsbmdm()) && !fzycPzVo.getSsbmdm().equals(vo.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(fzycPzVo.getSsbmdm());
            if (dept != null) {
                fzycPz.setSsbm(dept.getDeptname());
                fzycPz.setSsbmdm(dept.getDeptcode());
                fzycPz.setSsbmid(dept.getDeptid());
                fzycPz.setSsxgaj(dept.getFxjname());
                fzycPz.setSsxgajid(String.valueOf(dept.getFxjid()));
                fzycPz.setSsxgajdm(dept.getFxjcode());
                fzycPz.setSssgaj(dept.getDszname());
                fzycPz.setSssgajid(String.valueOf(dept.getDszid()));
                fzycPz.setSssgajdm(dept.getDszcode());
            }
        }
        return this.baseMapper.updateById(fzycPz);

    }

    @Override
    public TbFzycPz queryById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<TbFzycPz> queryAll() {
        return this.baseMapper.selectList(new QueryWrapper<>());
    }
}
