package com.tuoheng.service.impl;

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.constant.CommonConstant;
import com.tuoheng.enums.MarkTypeEnum;
import com.tuoheng.mapper.AreaMapper;
import com.tuoheng.mapper.CityMapper;
import com.tuoheng.model.dto.City;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.entity.Area;
import com.tuoheng.model.entity.Marker;
import com.tuoheng.model.query.AreaQuery;
import com.tuoheng.service.AreaService;
import com.tuoheng.until.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 小影
 * @description 针对表【t_area】的数据库操作Service实现
 * @createDate 2023-03-06 14:58:40
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area>
        implements AreaService {

    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private CityMapper cityMapper;


    /**
     * 查询区域信息
     *
     * @param query
     * @return
     */
    @Override
    public JsonResult index(AreaQuery query) {
        //分页参数校验
        query.checkParam();
        //开启分页
        IPage<Area> page = new Page<>(query.getPage(), query.getLimit());

        IPage<Area> areaPage = areaMapper.selectPage(page, Wrappers.<Area>lambdaQuery()
                .eq(Area::getMark, MarkTypeEnum.VALID.getCode())
                .like(ObjectUtil.isNotEmpty(query.getName()), Area::getName, query.getName())
                .orderByDesc(Area::getCreateTime));

        return JsonResult.success(areaPage);
    }

    /**
     * 新增区域信息
     *
     * @param entity
     * @return
     */
    @Override
    public JsonResult edit(Area entity, LoginUser loginUser) {

        if (ObjectUtil.isNull(entity.getId())) {
            //新增
            setCityName(entity);
            entity.setEditorName(loginUser.getUsername());
            entity.setCreateUser(loginUser.getUserId());
            this.save(entity);
        } else {
            //编辑
            setCityName(entity);
            entity.setEditorName(loginUser.getUsername());
            entity.setUpdateUser(loginUser.getUserId());
            this.updateById(entity);
        }

        return JsonResult.success();
    }

    /**
     * 更新区域
     *
     * @param entity
     */
    private void setCityName(Area entity) {
        if (entity.getCityCode().contains(CommonConstant.COMMA)) {
            StringBuilder cityName = new StringBuilder();
            for (String code : entity.getCityCode().split(CommonConstant.COMMA)) {
                City city = cityMapper.selectOne(Wrappers.<City>lambdaQuery()
                        .eq(City::getMark, MarkTypeEnum.VALID.getCode())
                        .eq(City::getCitycode, code));
                cityName.append(city.getName());
                cityName.append(CommonConstant.SIGN);
            }
            //切割拼接
            entity.setCityName(cityName.substring(CommonConstant.ZERO, cityName.toString().lastIndexOf(CommonConstant.SIGN)));
        } else {
            City city = cityMapper.selectOne(Wrappers.<City>lambdaQuery()
                    .eq(City::getMark, MarkTypeEnum.VALID.getCode())
                    .eq(City::getCitycode, entity.getCityCode()));
            entity.setCityName(city.getName());
        }
    }

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

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




