package com.tofly.scada.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.UserClent;
import com.tofly.pojo.User;
import com.tofly.scada.common.FlowConstant;
import com.tofly.scada.entity.ScadaReport;
import com.tofly.scada.entity.Syschildcode;
import com.tofly.scada.mapper.CodeDicMapper;
import com.tofly.scada.mapper.ScadaReportMapper;
import com.tofly.scada.mapper.UserXMapper;
import com.tofly.scada.service.ScadaReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
* @author haiqiu
* @description 针对表【TF_SCADA_REPORT】的数据库操作Service实现
* @createDate 2022-05-13 14:45:46
*/
@Service
public class ScadaReportServiceImpl extends ServiceImpl<ScadaReportMapper, ScadaReport> implements ScadaReportService{


    @Autowired
    private UserXMapper userXMapper;

    @Autowired
    private UserClent userClent;

    @Autowired
    private CodeDicMapper codeDicMapper;

    @Autowired
    private ScadaReportMapper scadaReportMapper;

    @Override
    public void insert(ScadaReport dto) {
        setDefaultValue(dto);
        if (!this.save(dto)){
            throw new RuntimeException("新增失败");
        }
    }


    /**
     * 检测重复数据
     * @param dto 操作数据实体
     */
//    private void checkData(EmergencyFacilitiesDTO dto) {
//        QueryWrapper<Emergency> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("IS_DEL", FlowConstant.SHORT_ZERO);
//        queryWrapper.eq("NAME", dto.getName());
//        queryWrapper.eq("PHONE", dto.getPhone());
//        Emergency one = this.getOne(queryWrapper);
//        if (!ObjectUtils.isEmpty(one)){
//            throw new RuntimeException(dto.getName() + "的手机已经存在，无需重复添加");
//        }
//    }

    @Override
    public void edit(ScadaReport dto) {
        setDefaultValue(dto);
        if (!this.updateById(dto)){
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public void delete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            throw new RuntimeException("传入数据为空");
        }
        boolean update = this.update(new ScadaReport(), new UpdateWrapper<ScadaReport>().lambda()
                .set(ScadaReport::getIsDel, FlowConstant.SHORT_ONE)
                .eq(ScadaReport::getIsDel, FlowConstant.SHORT_ZERO)
                .in(ScadaReport::getId, ids));
        if (!update){
            throw new RuntimeException("删除失败");
        }
    }



    @Override
    public Page<ScadaReport>  pageList(Page page, ScadaReport query){
        QueryWrapper<ScadaReport> queryWrapper = getEmergencyQueryWrapper(query);
//        return scadaReportMapper.selectPageList(page, queryWrapper);
        Page<ScadaReport> pageData = this.page(page,queryWrapper);
        if (pageData.getTotal()>0){
            for (ScadaReport record : pageData.getRecords()) {
                if (record.getLeaderPeople()!=null){
                        record.setLeaderName(getUser(record.getLeaderPeople()));
                }
                if (record.getLiaisonPeople()!=null){
                    record.setLiaisonName(getUser(record.getLiaisonPeople()));
                }
                if (record.getAllocationId()!=null){
                    QueryWrapper<Syschildcode> syschildcodeQueryWrapper = new QueryWrapper<>();
                    syschildcodeQueryWrapper.eq("PCID",60000)
                            .eq("CCODE",record.getAllocationId());
                    List<Syschildcode> syschildcodes = codeDicMapper.selectList(syschildcodeQueryWrapper);
                    record.setDataTypeName(!CollectionUtils.isEmpty(syschildcodes) ? syschildcodes.get(0).getCname() : null);
                }
            }
        }
//        int index = (int) (page.getCurrent()-1);
//        int size = (int) page.getSize();
//        PageHelper.startPage(index, size);
//        List<ScadaReport> scadaReports = scadaReportMapper.pageList(query);
//        PageInfo<ScadaReport> pageInfo = new PageInfo(scadaReports);
//        page.setRecords(pageInfo.getList());
//        page.setTotal(pageInfo.getTotal());
        return pageData;
    }

    public String getUser(Long id){
        User user = null;
        try {
            user = userClent.getById(id).getResult();
        }catch (Exception e){
            e.printStackTrace();
        }
        if (ObjectUtils.isEmpty(user)){
            return null;
        }
        return user.getRealName()== null ? user.getAliasname() : user.getRealName();
    }

    /**
     * 设置查询条件
     * @param query 查询参数
     * @return QueryWrapper
     */
    private QueryWrapper<ScadaReport> getEmergencyQueryWrapper(ScadaReport query) {
        ScadaReport emergencyFacilities = new ScadaReport();
        BeanUtils.copyProperties(query,emergencyFacilities);
        emergencyFacilities.setIsDel(FlowConstant.INTEGER_ZERO);
        QueryWrapper<ScadaReport> queryWrapper = Wrappers.query(emergencyFacilities);
        return queryWrapper;
    }

    @Override
    public List<ScadaReport> all(ScadaReport query) {
        QueryWrapper<ScadaReport> addressBookQueryWrapper = getEmergencyQueryWrapper(query);
        return this.list(addressBookQueryWrapper);
    }

    @Override
    public ScadaReport get(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * 设置默认值
     * @param dto 实体
     */
    private void setDefaultValue(ScadaReport dto) {
        dto.setIsDel(FlowConstant.INTEGER_ZERO);
//        dto.setIsValid(FlowConstant.SHORT_ONE);
    }
}




