package com.tuoheng.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.mapper.AreaMapper;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Area;
import com.tuoheng.model.entity.Marker;
import com.tuoheng.model.query.MarkerQuery;
import com.tuoheng.model.vo.MarkerVo;
import com.tuoheng.service.MarkerService;
import com.tuoheng.mapper.MarkerMapper;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 小影
 * @description 针对表【t_marker】的数据库操作Service实现
 * @createDate 2023-03-07 15:21:37
 */
@Service
public class MarkerServiceImpl extends ServiceImpl<MarkerMapper, Marker>
        implements MarkerService {
    @Autowired
    private MarkerMapper markerMapper;
    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询营销人员信息
     *
     * @param query
     * @return
     */
    @Override
    public JsonResult index(MarkerQuery query) {

        //校验分页参数
        query.checkParam();
        //开启分页
        IPage<Marker> page = new Page<>(query.getPage(), query.getLimit());
        IPage<Marker> pageDate = markerMapper.selectPage(page, Wrappers.<Marker>lambdaQuery()
                .eq(Marker::getMark, MarkTypeEnum.VALID.getCode())
                .like(ObjectUtil.isNotEmpty(query.getName()), Marker::getName, query.getName())
                .eq(ObjectUtil.isNotNull(query.getAreaId()), Marker::getAreaId, query.getAreaId())
                .orderByDesc(Marker::getCreateTime));
        //返回实体类进行处理
        pageDate.convert(x -> {
            MarkerVo vo = Convert.convert(MarkerVo.class, x);
            Area area = areaMapper.selectById(x.getAreaId());
            if (ObjectUtil.isNull(area)) {
                return JsonResult.error("查询不到对应的营销区域");
            }
            vo.setAreaName(area.getName());
            return vo;
        });
        return JsonResult.success(pageDate);
    }

    /**
     * 新增营销人员信息
     *
     * @param entity
     * @return
     */
    @Override
    public JsonResult edit(Marker entity, LoginUser loginUser) {

        if (ObjectUtil.isNull(entity.getId())) {
            //新增
            entity.setMark(1);
            //编辑人
            entity.setEditorName(loginUser.getUsername());
            entity.setCreateUser(loginUser.getUserId());
            this.save(entity);
        } else {
            //更新
            //编辑人
            entity.setEditorName(loginUser.getUsername());
            entity.setUpdateUser(loginUser.getUserId());
            this.updateById(entity);
        }
        return JsonResult.success();
    }

    /**
     * 逻辑删除/批量
     *
     * @param ids
     * @return
     */
    @Override
    public JsonResult deleteByIds(Long[] ids) {

        for (Long id : ids) {
            Marker marker = markerMapper.selectById(id);
            marker.setMark(MarkTypeEnum.NOTVALID.getCode());
            markerMapper.updateById(marker);
        }
        return JsonResult.success();
    }

    /**
     * 根据区域id查询出对应营销人员
     *
     * @param query
     * @return
     */
    @Override
    public JsonResult getMarkerList(MarkerQuery query) {

        if (ObjectUtil.isNull(query.getAreaId())){
            return JsonResult.error("区域id不能为空");
        }

        List<Marker> markers = markerMapper.selectList(Wrappers.<Marker>lambdaQuery()
                .eq(Marker::getMark, MarkTypeEnum.VALID.getCode())
                .eq(Marker::getAreaId, query.getAreaId()));
        return JsonResult.success(markers);
    }


}




