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


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.yszx.TbYsGajgDto;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.dto.jcgl.TbJcglBxxDto;
import com.mosty.base.model.entity.jcgl.TbJcglBxx;
import com.mosty.base.model.query.jcgl.TbJcglBxxQuery;
import com.mosty.base.manager.mapper.TbJcglBxxMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.remote.TbYsZxAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglBxxService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengbo
 * @since 2022-07-16
 */
@AllArgsConstructor
@Service
public class TbJcglBxxServiceImpl extends ServiceImpl<TbJcglBxxMapper, TbJcglBxx>
        implements TbJcglBxxService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbYsZxAdaptRemoteService tbYsZxAdaptRemoteService;

    @Override
    @Transactional
    public int addBxx(TbJcglBxxDto bxx) {
        TbJcglBxx item = new TbJcglBxx();
        BeanUtils.copyProperties(bxx, item);
        if (!CollectionUtils.isEmpty(bxx.getZb())) {
            item.setZb(JtsUtils.createLine(bxx.getZb()));
        }
        if (StringUtils.isNotBlank(item.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            item.setSsbm(dept.getDeptname());
            item.setSsbmid(dept.getDeptid());
            item.setSsbmdm(dept.getDeptcode());
            item.setSsxgajdm(dept.getFxjcode());
            item.setSsxgajid(dept.getFxjid());
            item.setSsxgaj(dept.getFxjname());
            item.setSssgaj(dept.getDszname());
            item.setSssgajdm(dept.getDszcode());
            item.setSssgajid(dept.getDszid());
        }
        item.setXtSjly("1");
        int ret = this.baseMapper.insertEntity(item);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(new BigDecimal(item.getId()).toPlainString());
        ysGajgDto.setLybm("tb_jcgl_bxx");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001012");
        ysGajgDto.setGajgmc(item.getBxxMc());
        ysGajgDto.setGajgjc(item.getBxxMc());
        ysGajgDto.setWzXzqh(item.getXzqhdm());
        ysGajgDto.setSsbm(item.getSsbm());
        ysGajgDto.setSsbmid(item.getSsbmid());
        ysGajgDto.setSsbmdm(item.getSsbmdm());
        tbYsZxAdaptRemoteService.addGajg(ysGajgDto);
        return ret;
    }

    @Override
    public IPage<TbJcglBxxDto> queryBxx(TbJcglBxxQuery dto) {
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
//        UserInfo userInfo = UserInfoManager.get();
//        String useSql = PermissionsUtil.createSql("", userInfo);
        IPage<TbJcglBxxDto> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
//        map.put("useSql", useSql);
        int count = this.baseMapper.getCount(map);
        List<TbJcglBxx> list = this.baseMapper.getPageList(map);
        List<TbJcglBxxDto> rList = new ArrayList<>();
        list.forEach(item -> {
            TbJcglBxxDto vo = new TbJcglBxxDto();
            BeanUtils.copyProperties(item, vo);
            if (item.getZb() != null) vo.setZb(JtsUtils.decodeLine(item.getZb()));
            rList.add(vo);
        });
        page.setRecords(rList);
        page.setTotal(count);
        return page;
    }

    @Override
    @Transactional
    public int editBxx(TbJcglBxxDto bxx) {
        TbJcglBxx item = new TbJcglBxx();
        BeanUtils.copyProperties(bxx, item);
        if (!CollectionUtils.isEmpty(bxx.getZb())) {
            item.setZb(JtsUtils.createLine(bxx.getZb()));
        }
        if (StringUtils.isNotBlank(item.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            item.setSsbm(dept.getDeptname());
            item.setSsbmid(dept.getDeptid());
            item.setSsbmdm(dept.getDeptcode());
            item.setSsxgajdm(dept.getFxjcode());
            item.setSsxgajid(dept.getFxjid());
            item.setSsxgaj(dept.getFxjname());
            item.setSssgaj(dept.getDszname());
            item.setSssgajdm(dept.getDszcode());
            item.setSssgajid(dept.getDszid());
        }
        int ret = this.baseMapper.updateEntity(item);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(new BigDecimal(item.getId()).toPlainString());
        ysGajgDto.setLybm("tb_jcgl_bxx");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001012");
        ysGajgDto.setGajgmc(item.getBxxMc());
        ysGajgDto.setGajgjc(item.getBxxMc());
        ysGajgDto.setWzXzqh(item.getXzqhdm());
        ysGajgDto.setSsbm(item.getSsbm());
        ysGajgDto.setSsbmid(item.getSsbmid());
        ysGajgDto.setSsbmdm(item.getSsbmdm());
        tbYsZxAdaptRemoteService.updateGajg(ysGajgDto);
        return ret;
    }

    @Override
    @Transactional
    public int deleteBxx(List<String> list) {
        int ret = this.baseMapper.deleteBatch(list);
        for (String s : list) {
            TbYsGajgDto ysGajgDto = new TbYsGajgDto();
            ysGajgDto.setLyid(s);
            ysGajgDto.setLybm("tb_jcgl_bxx");
            tbYsZxAdaptRemoteService.delEntity(ysGajgDto);
        }
        return ret;
    }

    @Override
    public TbJcglBxxDto selectById(String id) {
        TbJcglBxx bxx = this.baseMapper.getById(id);
        TbJcglBxxDto dto = new TbJcglBxxDto();
        BeanUtils.copyProperties(bxx, dto);
        if (bxx.getZb() != null) dto.setZb(JtsUtils.decodeLine(bxx.getZb()));
        return dto;
    }
}
