package cn.iocoder.yudao.module.app.car.service.impl;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.app.car.entity.AppCarMain;
import cn.iocoder.yudao.module.app.car.mapper.AppCarMainMapper;
import cn.iocoder.yudao.module.app.car.service.AppCarMainService;
import cn.iocoder.yudao.module.app.car.vo.AppCarMainResVo;
import cn.iocoder.yudao.module.custom.entity.CustomerCar;
import cn.iocoder.yudao.module.custom.entity.CustomerMain;
import cn.iocoder.yudao.module.custom.mapper.CustomerCarMapper;
import cn.iocoder.yudao.module.custom.mapper.CustomerMainMapper;
import cn.iocoder.yudao.module.custom.vo.CarMainRespVO;
import cn.iocoder.yudao.module.label.service.BusiLabelService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 车辆信息 Service 实现类
 *
 * @author 后台管理员
 */
@Service
public class AppCarMainServiceImpl extends ServiceImpl<AppCarMainMapper, AppCarMain> implements AppCarMainService {

    @Autowired
    private CustomerCarMapper customerCarMapper;
    @Autowired
    private CustomerMainMapper customerMainMapper;

    /**
     * 创建车辆信息
     *
     * @param carMain 创建信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> createCarMain(AppCarMain carMain) {

        baseMapper.insert(carMain);
//      获取当前登录用户
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
//      根据当前登录用户id 查询  base_customer_main 的id
        HashMap<String, Object> m = new HashMap<>();
        m.put("user_id", loginUser.getId());
        List<CustomerMain> customerMains = customerMainMapper.selectByMap(m);
//      关联表添加数据
        CustomerCar customerCar = new CustomerCar();
        customerCar.setCarId(carMain.getId());
        customerCar.setCusId(customerMains.get(0).getId());
        customerCarMapper.insert(customerCar);

        // 返回
        return CommonResult.success("新增成功");
    }

    /**
     * 更新车辆信息
     *
     * @param carMain 更新信息
     */
    @Override
    public CommonResult<String> updateCarMain(AppCarMain carMain) {
        baseMapper.updateById(carMain);
        return CommonResult.success("修改成功");
    }

    /**
     * 删除车辆信息
     *
     * @param id 编号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> deleteCarMain(String id) {
//      删除主表
        baseMapper.deleteById(id);
        LambdaUpdateWrapper<CustomerCar> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CustomerCar::getCarId, id);
        CustomerCar customerCar = new CustomerCar();
        customerCar.setDeleted(true);
//      删除关联表
        customerCarMapper.update(customerCar, updateWrapper);
        return CommonResult.success("删除成功");
    }

    /**
     * 获得车辆信息
     *
     * @return
     */
    @Override
    public List<AppCarMainResVo> getCarMain() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        List<AppCarMainResVo> userCarMain = super.baseMapper.getUserCarMain(loginUser.getId());


        return userCarMain;
    }




}