package com.sckw.address.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.sckw.address.entity.KwBusinessAddressType;
import com.sckw.address.entity.vo.req.KwBusinessAddressTypeInsertParam;
import com.sckw.address.entity.vo.res.KwBusinessAddressTypeListRes;
import com.sckw.address.mapper.KwBusinessAddressTypeMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.web.response.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-04-12
 */
@Service
public class KwBusinessAddressTypeService {
    @Autowired
    KwBusinessAddressTypeMapper kwBusinessAddressTypeMapper;

    /**
    * @Description: 地址类型新增
    * @Author: Lt
    * @Date: 2024/4/12 9:09
    */
    public HttpResult addressTypeInsert(KwBusinessAddressTypeInsertParam kwBusinessAddressTypeInsertParam)
    {

        LambdaQueryWrapper<KwBusinessAddressType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KwBusinessAddressType::getName, kwBusinessAddressTypeInsertParam.getName())
                .eq(KwBusinessAddressType::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessAddressType addressTypeExists = kwBusinessAddressTypeMapper.selectOne(queryWrapper);
        if (ObjectUtils.isNotNull(addressTypeExists)) {
            return HttpResult.error("名称已存在");
        }
        KwBusinessAddressType kwBusinessAddressType = BeanUtils.copyProperties(kwBusinessAddressTypeInsertParam, KwBusinessAddressType.class);
        int insert = kwBusinessAddressTypeMapper.insert(kwBusinessAddressType);
        if (insert > 0) {
            return HttpResult.ok("地址添加成功");
        }
        return HttpResult.error("地址添加失败");
    }


    /**
     * @Description: 地址类型编辑
     * @Author: Lt
     * @Date: 2024/4/11 16:08
     */
    public HttpResult addressTypeUpdate(KwBusinessAddressTypeInsertParam kwBusinessAddressTypeInsertParam)
    {

        if(kwBusinessAddressTypeInsertParam.getId() == null) {
            return HttpResult.error("缺少参数:ID");
        }

        KwBusinessAddressType kwBusinessAddressTypeExists = kwBusinessAddressTypeMapper.selectOne(
                new LambdaQueryWrapper<KwBusinessAddressType>()
                        .eq(KwBusinessAddressType::getName, kwBusinessAddressTypeInsertParam.getName())
                        .ne(KwBusinessAddressType::getId, kwBusinessAddressTypeInsertParam.getId())
                        .eq(KwBusinessAddressType::getDelFlag, Global.NUMERICAL_ZERO));
        if (ObjectUtils.isNotNull(kwBusinessAddressTypeExists)) {
            return HttpResult.error("地址类型名称已存在");
        }

        KwBusinessAddressType oldKwBusinessAddress = kwBusinessAddressTypeMapper.selectOne(new QueryWrapper<KwBusinessAddressType>()
                .eq("id",kwBusinessAddressTypeInsertParam.getId()));

        if(oldKwBusinessAddress!=null){
            KwBusinessAddressType kwBusinessAddressType = BeanUtils.copyProperties(kwBusinessAddressTypeInsertParam, KwBusinessAddressType.class);
            kwBusinessAddressTypeMapper.updateById(kwBusinessAddressType);
            return HttpResult.ok("编辑成功");
        }
        return HttpResult.error("编辑失败");
    }

    /**
     * @Description: 地址查询
     * @Author: Lt
     * @Date: 2024/4/11 0011 17:09
     */
    public List<KwBusinessAddressTypeListRes> selectList(String name)
    {
        return kwBusinessAddressTypeMapper.list(name);
    }
    
    /**
    * @Description: 地址删除
    * @Author: Lt
    * @Date: 2024/4/12 0012 9:23
    */
    public HttpResult addressTypeDelete(String id)
    {
        KwBusinessAddressType kwBusinessAddress = kwBusinessAddressTypeMapper.selectOne(new QueryWrapper<KwBusinessAddressType>().eq("id",id).eq("del_flag",Global.NUMERICAL_ZERO));
        if(kwBusinessAddress!=null){
            kwBusinessAddress.setId(Long.valueOf(id));
            kwBusinessAddress.setDelFlag(Global.NUMERICAL_ONE);
            kwBusinessAddressTypeMapper.updateById(kwBusinessAddress);
            return HttpResult.ok("删除成功");
        }else{
            return HttpResult.error("没有找到该对象");
        }
    }


}
