/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.daily.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.UserClent;
import com.tofly.common.core.constant.ResponeStatus;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.IsUseEnum;
import com.tofly.gpsboot.mapper.plan.PlanregionMapper;
import com.tofly.gpsboot.service.daily.PlanregionService;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.Planregion;
import com.tofly.pojo.plan.dto.PlanregionDto;
import com.tofly.pojo.plan.vo.PlanregionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 巡检片区信息表：TF_YWPN_PLANREGION_W
 *
 * @author sunjian
 * @date Tue Mar 07 00:00:00 CST 2023
 */
@Service("planregionService")
public class PlanregionServiceImpl extends ServiceImpl<PlanregionMapper, Planregion> implements PlanregionService {

    @Autowired
    private UserClent userClent;


    /**
     * 视图转换 entity转换vo
     *
     * @param infoList
     * @return
     */
    @Override
    public List<PlanregionVo> viewConversion(List<Planregion> infoList) {
        List<PlanregionVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            PlanregionVo baseInfoVo = JSON.parseObject(JSON.toJSONString(info), PlanregionVo.class);

            baseInfoVo.setIsUse(IsUseEnum.getCodeForValue(info.getIsUse()));
            User result = userClent.getById(info.getCreateUser()).getResult();
            if (null != result) {
                baseInfoVo.setCreateUserName(result.getRealName());
            }
            infoVoList.add(baseInfoVo);
        });
        return infoVoList;
    }

    /**
     * 自定义分页查询
     *
     * @param page
     * @param planregionDto
     * @return
     */
    @Override
    public Page<PlanregionVo> getPlanPointPage(Page page, PlanregionDto planregionDto) {
        Planregion planregion = JSON.parseObject(JSON.toJSONString(planregionDto), Planregion.class);
        QueryWrapper<Planregion> wrapper = new QueryWrapper();
        wrapper.setEntity(planregion);
        //wrapper.lambda().eq(Planregion::getIsdelete,"0");
        wrapper.orderByDesc("CREATE_TIME");
        Page infoPageList = this.page(page, wrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    /**
     * 批量删除id（逻辑删除）
     *
     * @param ids
     * @return
     */
    @Override
    public ResultRespone<Object> removeByIds(String ids) {
        ResultRespone<Object> resultRespone = new ResultRespone<>();
        if (StringUtils.isNotEmpty(ids)) {
            try {
                if (!this.update(new UpdateWrapper<Planregion>().lambda().in(Planregion::getId, ids).set(Planregion::getIsdelete, "1"))) {
                    log.error(String.format("修改基本信息对象失败:ids = %s", ids));
                    resultRespone.setMessage(Constant.UPDATE_FAIL);
                    resultRespone.setCode(Constant.UPDATE_FAIL_CODE);
                }
            } catch (Exception e) {
                log.error(String.format("修改基本信息异常:e = %s", e));
                resultRespone.setCode(ResponeStatus.FAIL.getCode());
            }
        }
        return resultRespone;
    }
}
