package com.mosty.yjzl.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.entity.yjzl.TbYjmx;
import com.mosty.base.model.entity.yjzl.TbYjmxCsb;
import com.mosty.base.model.entity.yjzl.TbYjxx;
import com.mosty.base.model.query.yjzl.TbYjmxQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.yjzl.TbYjmxAllVo;
import com.mosty.base.model.vo.yjzl.TbYjmxCsbVo;
import com.mosty.base.model.vo.yjzl.TbYjmxVo;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.yjzl.mapper.TbYjmxCsbMapper;
import com.mosty.yjzl.mapper.TbYjmxMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbYjmxService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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


@Service
@AllArgsConstructor
public class TbYjmxServiceImpl extends ServiceImpl<TbYjmxMapper, TbYjmx>
        implements TbYjmxService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbYjmxCsbMapper tbYjmxCsbMapper;


    @Override
    public int insert(TbYjmxVo vo) {
        TbYjmx mx = this.baseMapper.selectById(vo.getId());
        if (mx != null) {
            throw new BusinessException("模型ID已存在！");
        }
        TbYjmx yjmx1 = new TbYjmx();
        BeanUtils.copyProperties(vo, yjmx1);
        yjmx1.setMxzt("01");
        yjmx1.setXtSjly("1");
        if (StringUtils.isNotBlank(vo.getSsbmid())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(vo.getSsbmid());
            if (dept != null) {
                yjmx1.setSsbm(dept.getDeptname());
                yjmx1.setSsbmdm(dept.getDeptcode());
            }
        }
        return baseMapper.insert(yjmx1);
    }

    @Override
    public int updateById(TbYjmxVo vo) {
        TbYjmx yjmx1 = new TbYjmx();
        BeanUtils.copyProperties(vo, yjmx1);
        yjmx1.setXtSjly("1");
        if (StringUtils.isNotBlank(vo.getSsbmid())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(vo.getSsbmid());
            if (dept != null) {
                yjmx1.setSsbm(dept.getDeptname());
                yjmx1.setSsbmdm(dept.getDeptcode());
            }
        }
        return baseMapper.updateById(yjmx1);
    }

    @Override
    public TbYjmxVo queryById(String id) {
        TbYjmx yjmx = baseMapper.selectById(id);
        TbYjmxVo vo = new TbYjmxVo();
        BeanUtils.copyProperties(yjmx, vo);
        return vo;
    }


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

    @Override
    public IPage<TbYjmxVo> getPage(TbYjmxQuery dto) {
        IPage<TbYjmx> page = this.baseMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbYjmx>()
                        .like(StringUtils.isNotBlank(dto.getMxmc()), TbYjmx::getMxmc, dto.getMxmc())
                        .eq(StringUtils.isNotBlank(dto.getMxlx()), TbYjmx::getMxlx, dto.getMxlx())
                        .like(StringUtils.isNotBlank(dto.getSsbm()), TbYjmx::getSsbm, dto.getSsbm())
                        .eq(StringUtils.isNotBlank(dto.getSsbmdm()), TbYjmx::getSsbmdm, dto.getSsbmdm())
                        .eq(StringUtils.isNotBlank(dto.getSsbmid()), TbYjmx::getSsbmid, dto.getSsbmid())
                        .eq(StringUtils.isNotBlank(dto.getMxzt()), TbYjmx::getMxzt, dto.getMxzt())
        );
        IPage<TbYjmxVo> pageVo = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbYjmxVo> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbYjmxVo vo = new TbYjmxVo();
                BeanUtils.copyProperties(item, vo);
                voList.add(vo);
            });
        }
        pageVo.setRecords(voList);
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    @Override
    public int startMx(String id) {
        TbYjmx yjmx = baseMapper.selectById(id);
        TbYjmxVo vo = new TbYjmxVo();
        BeanUtils.copyProperties(yjmx, vo);
        if (yjmx.getMxzt().equals("01")) {
            yjmx.setMxzt("02");
        } else if (yjmx.getMxzt().equals("02")) {
            yjmx.setMxzt("01");
        }
        return this.baseMapper.updateById(yjmx);
    }

    @Override
    public TbYjmxAllVo getMxCs(String mxid) {
        TbYjmx mx = this.baseMapper.selectById(mxid);
        if (mx == null) return null;
        TbYjmxAllVo vo = new TbYjmxAllVo();
        BeanUtils.copyProperties(mx, vo);
        List<TbYjmxCsb> csList = this.tbYjmxCsbMapper.selectList(
                new LambdaQueryWrapper<TbYjmxCsb>()
                        .eq(TbYjmxCsb::getMxid, mxid)
                        .eq(TbYjmxCsb::getXtSjzt, "1")
        );
        List<TbYjmxCsbVo> voList = new ArrayList<>();
        csList.forEach(item -> {
            TbYjmxCsbVo csVo = new TbYjmxCsbVo();
            BeanUtils.copyProperties(item, csVo);
            voList.add(csVo);
        });
        vo.setCsList(voList);
        return vo;
    }


}
