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.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.address.entity.KwBusinessAddress;
import com.sckw.address.entity.vo.req.KwBusinessAddressListParam;
import com.sckw.address.entity.vo.req.KwBusinessAddressPageListParam;
import com.sckw.address.entity.vo.req.KwBusinessAddressParam;
import com.sckw.address.entity.vo.res.KwBusinessAddressListRes;
import com.sckw.address.entity.vo.res.KwBusinessAddressTypeListRes;
import com.sckw.address.entity.vo.res.KwBusinessPageListRes;
import com.sckw.address.mapper.KwBusinessAddressMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.response.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-04-11
 */
@Service
public class KwAddressService {

    @Autowired
    KwBusinessAddressMapper kwBusinessAddressMapper;

    @Autowired
    KwBusinessAddressTypeService kwBusinessAddressTypeService;

    /**
    * @Description: 订单新增
    * @Author: Lt
    * @Date: 2024/4/11 16:02
    */
    public HttpResult addressCreate(KwBusinessAddressParam kwBusinessAddressParam)
    {
        KwBusinessAddress kwBusinessAddress = BeanUtils.copyProperties(kwBusinessAddressParam, KwBusinessAddress.class);
        KwBusinessAddress name = kwBusinessAddressMapper.
                selectOne(new QueryWrapper<KwBusinessAddress>().eq("name", kwBusinessAddress.getName()));
        if(ObjectUtils.isNotEmpty(name)){
            return HttpResult.error("名称重复");
        }
        kwBusinessAddressMapper.insert(kwBusinessAddress);
        return HttpResult.ok("保存成功");
    }

    /**
    * @Description: 地址编辑
    * @Author: Lt
    * @Date: 2024/4/11 16:08
    */
    public HttpResult addressUpdate(KwBusinessAddressParam kwBusinessAddressParam)
    {

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

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


        KwBusinessAddress oldKwBusinessAddress = kwBusinessAddressMapper.selectOne(new QueryWrapper<KwBusinessAddress>()
                .eq("id",kwBusinessAddressParam.getId()));
        
        if(oldKwBusinessAddress!=null){
            KwBusinessAddress kwBusinessAddress = BeanUtils.copyProperties(kwBusinessAddressParam, KwBusinessAddress.class);
            kwBusinessAddressMapper.updateById(kwBusinessAddress);
            return HttpResult.ok("编辑成功");
        }
        return HttpResult.error("编辑失败");
    }

    /**
    * @Description: 地址删除
    * @Author: Lt
    * @Date: 2024/4/11 16:35
    */
    public HttpResult addressDelete(String id)
    {
        KwBusinessAddress kwBusinessAddress = kwBusinessAddressMapper.selectOne(new QueryWrapper<KwBusinessAddress>().eq("id",id).eq("del_flag",Global.NUMERICAL_ZERO));
        if(kwBusinessAddress!=null){
            kwBusinessAddress.setId(Long.valueOf(id));
            kwBusinessAddress.setDelFlag(Global.NUMERICAL_ONE);
            kwBusinessAddressMapper.updateById(kwBusinessAddress);
            return HttpResult.ok("删除成功");
        }else{
            return HttpResult.error("没有找到该对象");
        }
    }

    /**
    * @Description: 地址Find查询
    * @Author: Lt
    * @Date: 2024/4/11 0011 16:39
    */
    public HttpResult addressFind(String id)
    {
        LambdaQueryWrapper<KwBusinessAddress> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(KwBusinessAddress::getId, id).eq(KwBusinessAddress::getDelFlag,Global.NUMERICAL_ZERO);
        KwBusinessAddress kwBusinessAddress = kwBusinessAddressMapper.selectOne(addressLambdaQueryWrapper);
        if(kwBusinessAddress!=null){
            return HttpResult.ok(kwBusinessAddress);
        }else{
            return HttpResult.error("没有找到该对象");
        }

    }

    /**
    * @Description: 地址分页查询
    * @Author: Lt
    * @Date: 2024/4/11 16:46
    */
    public HttpResult addressPageList(KwBusinessAddressPageListParam kwBusinessAddressListParam)
    {
        PageHelper.startPage(kwBusinessAddressListParam.getPage(), kwBusinessAddressListParam.getPageSize());
        List<KwBusinessPageListRes> kwBusinessPageListRes = kwBusinessAddressMapper.selectPageList(kwBusinessAddressListParam);
        HashMap<Long, String> objectObjectHashMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(kwBusinessPageListRes)) {
            List<KwBusinessAddressTypeListRes> kwBusinessAddressTypeListRes = kwBusinessAddressTypeService.selectList(null);
            kwBusinessAddressTypeListRes.forEach(l -> {
                objectObjectHashMap.put(l.getId(), l.getName());
            });
        }

        kwBusinessPageListRes.forEach(e -> {
            String typeStr = objectObjectHashMap.get(e.getType());
            if (typeStr != null) {
                e.setTypeStr(typeStr);
            }
        });
        return HttpResult.ok(new PageRes<>(new PageInfo<>(kwBusinessPageListRes)));
    }

    /**
    * @Description: 地址查询
    * @Author: Lt
    * @Date: 2024/4/11 0011 17:09
    */
    public PageRes<KwBusinessAddressListRes> selectList(KwBusinessAddressListParam param)
    {

        List<KwBusinessAddressListRes> resultList;

        // 判断是否需要分页
        if (param.getPage() != null && param.getPageSize() != null) {
            // 分页查询
            PageHelper.startPage(param.getPage(), param.getPageSize());
            resultList = kwBusinessAddressMapper.list(param);
            return new PageRes<>(new PageInfo<>(resultList)); // 注意这里应该返回resultList而不是kwBusinessGoods
        } else {
            // 不分页查询
            resultList = kwBusinessAddressMapper.list(param);
            // 直接返回结果列表，不包装PageInfo，除非你有其他非分页的PageRes构造方式
            return new PageRes<>(resultList); // 假设PageRes有合适的构造方法处理非分页情况
        }
    }


}
