package cn.iocoder.yudao.module.custom.service.impl;

import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.custom.entity.CustomerItem;
import cn.iocoder.yudao.module.custom.entity.CustomerMain;
import cn.iocoder.yudao.module.custom.mapper.CustomerMainMapper;
import cn.iocoder.yudao.module.custom.service.CustomerItemService;
import cn.iocoder.yudao.module.custom.service.CustomerMainService;
import cn.iocoder.yudao.module.custom.vo.CustomerMainPageReqVO;
import cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO;
import cn.iocoder.yudao.module.custom.vo.CustomerMainSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

import static cn.iocoder.yudao.common.BaseConstants.CUS_TYPE_CORP_ATTN;

/**
 * 客户管理 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class CustomerMainServiceImpl extends ServiceImpl<CustomerMainMapper, CustomerMain> implements CustomerMainService {

    @Resource
    private CustomerMainMapper customerMainMapper;
    @Resource
    private CustomerItemService customerItemService;

    /**
     * 客户管理分页列表查询
     *
     * @param pageReqVO 客户管理查询条件封装实体
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO>
     * @author PQZ
     * @date 14:57 2024/8/1
     **/
    @Override
    public IPage<CustomerMainRespVO> queryListPage(CustomerMainPageReqVO pageReqVO, Page<CustomerMainRespVO> page) {
        return customerMainMapper.selectListPage(pageReqVO, page);
    }

    /**
     * 保存客户信息
     *
     * @param saveReqVO 保存客户信息扩展实体
     * @return void
     * @author PQZ
     * @date 15:46 2024/8/1
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCustomer(CustomerMainSaveReqVO saveReqVO) {
        try {
            /*1、保存主表信息*/
            CustomerMain main = JSONUtil.toBean(JSONUtil.parseObj(saveReqVO).toJSONString(0), CustomerMain.class);
            //新增情况下，非政企客户经办人，deptCode设置为id，方便分组查询
            if (null == main.getId() && !CUS_TYPE_CORP_ATTN.equals(main.getTypeCode())) {
                String id = String.valueOf(UUID.randomUUID());
                main.setId(id);
                main.setDeptCode(id);
            }
            this.saveOrUpdate(main);
            /*2、保存扩展表信息*/
            if (!saveReqVO.getItemList().isEmpty()) {
                customerItemService.saveCutomItem(main.getId(), saveReqVO.getItemList());
            }
        } catch (ServiceException e) {
            log.error(e.getMessage());
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
        }

    }

    /**
     * 根据客户id查询客户信息
     *
     * @param id 客户id
     * @return cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO
     * @author PQZ
     * @date 15:12 2024/8/2
     **/
    @Override
    public CustomerMainRespVO getCustomerById(String id) {
        /*1、主表信息*/
        CustomerMain main = this.getById(id);
        /*2、扩展表信息*/
        LambdaQueryWrapper<CustomerItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted, 0).eq(CustomerItem::getCusId, id);
        List<CustomerItem> itemList = customerItemService.list(lambdaQueryWrapper);
        CustomerMainRespVO result = JSONUtil.toBean(JSONUtil.parseObj(main).toJSONString(0), CustomerMainRespVO.class);
        result.setItemList(itemList);
        return result;
    }

    /**
     * 根据经办人所属企业查询经办人信息
     *
     * @param deptCode 经办人所属企业code
     * @return java.util.List<cn.iocoder.yudao.module.custom.vo.CustomerMainRespVO>
     * @author PQZ
     * @date 16:15 2024/8/2
     **/
    @Override
    public List<CustomerMain> getCustomerByDeptCode(String deptCode) {
        LambdaQueryWrapper<CustomerMain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseDO::getDeleted, 0).eq(CustomerMain::getDeptCode, deptCode).eq(CustomerMain::getTypeCode, CUS_TYPE_CORP_ATTN);
        return list(lambdaQueryWrapper);
    }
}