package com.sckw.driver.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageRes;
import com.sckw.driver.entity.KwBusinessDriver;
import com.sckw.driver.entity.req.KwBusinessDriverInsertParam;
import com.sckw.driver.entity.req.KwBusinessDriverPageListParam;
import com.sckw.driver.entity.req.KwBusinessDriverUpdateParam;
import com.sckw.driver.entity.res.KwBusinessDriverDetailRes;
import com.sckw.driver.entity.res.KwBusinessDriverPageListRes;
import com.sckw.driver.mapper.KwBusinessDriverMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.*;
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 KwBusinessDriverService {

    @Autowired
    KwBusinessDriverMapper kwBusinessDriverMapper;

    /**
    * @Description: 司机新增
    * @Author: Lt
    * @Date: 2024/4/12 13:50
    */
    public HttpResult driverInsert(KwBusinessDriverInsertParam kwBusinessDriverInsertParam)
    {
        KwBusinessDriver kwBusinessDriver = BeanUtils.copyProperties(kwBusinessDriverInsertParam, KwBusinessDriver.class);
        KwBusinessDriver name = kwBusinessDriverMapper.
                selectOne(new LambdaQueryWrapper<KwBusinessDriver>()
                        .eq(KwBusinessDriver::getIdcard, kwBusinessDriverInsertParam.getIdcard())
                        .eq(KwBusinessDriver::getDelFlag, Global.NUMERICAL_ZERO)
                );
        if(ObjectUtils.isNotNull(name)){
            return HttpResult.error("证件号重复");
        }
        kwBusinessDriverMapper.insert(kwBusinessDriver);
        return HttpResult.ok("保存成功");
    }

    /**
     * @Description: 司机编辑
     * @Author: Lt
     * @Date: 2024/4/11 16:08
     */
    public HttpResult driverUpdate(KwBusinessDriverUpdateParam kwBusinessDriverUpdateParam) {
        Long id = kwBusinessDriverUpdateParam.getId();
        if (id == null) {
            return HttpResult.error("缺少参数:ID");
        }
        KwBusinessDriver kwBusinessAddressTypeExists = kwBusinessDriverMapper.selectOne(
                new LambdaQueryWrapper<KwBusinessDriver>()
                        .eq(KwBusinessDriver::getIdcard, kwBusinessDriverUpdateParam.getIdcard())
                        .ne(KwBusinessDriver::getId, id)
                        .eq(KwBusinessDriver::getDelFlag, Global.NUMERICAL_ZERO)
        );
        if (ObjectUtils.isNotNull(kwBusinessAddressTypeExists)) {
            return HttpResult.error("当前证件已存在");
        }

        KwBusinessDriver kwBusinessAddress = BeanUtils.copyProperties(kwBusinessDriverUpdateParam, KwBusinessDriver.class);
        kwBusinessAddress.setId(id);
        int rows = kwBusinessDriverMapper.updateById(kwBusinessAddress);
        if (rows > 0) {
            return HttpResult.ok("编辑成功");
        } else {
            return HttpResult.error("编辑失败");
        }
    }

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

    /**
    * @Description: 司机详情
    * @Author: Lt
    * @Date: 2024/4/12 15:35
    */
    public KwBusinessDriverDetailRes driverDetail(String id)
    {
         KwBusinessDriverDetailRes driverDetailById = kwBusinessDriverMapper.getDriverDetailById(id);
         return driverDetailById;
    }


    /**
     * @return
     * @Description: 地址分页查询
     * @Author: Lt
     * @Date: 2024/4/11 16:46
     */
    public PageRes<KwBusinessDriverPageListRes> driverPageList(KwBusinessDriverPageListParam kwBusinessDriverPageListParam)
    {
        PageHelper.startPage(kwBusinessDriverPageListParam.getPage(), kwBusinessDriverPageListParam.getPageSize());
        List<KwBusinessDriverPageListRes> kwBusinessPageListRes = kwBusinessDriverMapper.selectPageList(kwBusinessDriverPageListParam);
        //HashMap<Long, String> objectObjectHashMap = new HashMap<>();
        //if(CollectionUtils.isNotEmpty(kwBusinessPageListRes)) {
        //    List<KwBusinessDriverPageListRes> kwBusinessAddressTypeListRes = kwBusinessDriverService.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 new PageRes<>(new PageInfo<>(kwBusinessPageListRes));
    }

}
