package cn.iocoder.yudao.module.visit.service.info;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.visit.controller.admin.customerinfo.vo.CustomerInfoSaveReqVO;
import cn.iocoder.yudao.module.visit.controller.admin.info.vo.InfoPageReqVO;
import cn.iocoder.yudao.module.visit.controller.admin.info.vo.InfoSaveReqVO;
import cn.iocoder.yudao.module.visit.dal.dataobject.customerinfo.CustomerInfoDO;
import cn.iocoder.yudao.module.visit.dal.dataobject.info.InfoDO;
import cn.iocoder.yudao.module.visit.dal.mysql.customerinfo.CustomerInfoMapper;
import cn.iocoder.yudao.module.visit.dal.mysql.info.InfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.visit.enums.ErrorCodeConstants.INFO_NOT_EXISTS;

/**
 * 客户拜访记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;

    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createInfo(InfoSaveReqVO createReqVO) {
        // 插入
        InfoDO info = BeanUtils.toBean(createReqVO, InfoDO.class);
        int insert = infoMapper.insert(info);
        if (insert >0){
            //拜访记录插入成功，根据【公司名称】判断该客户是否记录在案，如果没有，则插入一条客户信息
            CustomerInfoDO customerInfoDO = customerInfoMapper.selectByCompanyName(info.getCompanyName());
            if (customerInfoDO == null){
                //没查到说明该客户没有记录，插入一条客户信息
                CustomerInfoSaveReqVO customerInfoSaveReqVO = new CustomerInfoSaveReqVO();
                customerInfoSaveReqVO.setCustomerName(info.getCustomerName());
                customerInfoSaveReqVO.setContact(info.getContact());
                customerInfoSaveReqVO.setCompanyName(info.getCompanyName());
                customerInfoSaveReqVO.setCustomerType(info.getCustomerStatus());
                customerInfoSaveReqVO.setProvinceName(info.getProvinceName());
                customerInfoSaveReqVO.setCityName(info.getCityName());
                customerInfoSaveReqVO.setAreaName(info.getAreaName());
                customerInfoSaveReqVO.setRegionFullName(info.getRegionFullName());
                customerInfoSaveReqVO.setLocationText(info.getLocationText());
                customerInfoSaveReqVO.setLongitude(info.getLongitude());
                customerInfoSaveReqVO.setLatitude(info.getLatitude());
                customerInfoSaveReqVO.setLocationImage(info.getLocationImage());
                customerInfoSaveReqVO.setProductIds(info.getVisitProductIds());
                customerInfoSaveReqVO.setProductNames(info.getVisitProductNames());
                customerInfoMapper.insert(BeanUtils.toBean(customerInfoSaveReqVO, CustomerInfoDO.class));
            }
        }

        // 返回
        return info.getId();
    }

    @Override
    public void updateInfo(InfoSaveReqVO updateReqVO) {
        // 校验存在
        validateInfoExists(updateReqVO.getId());
        // 更新
        InfoDO updateObj = BeanUtils.toBean(updateReqVO, InfoDO.class);
        infoMapper.updateById(updateObj);
    }

    @Override
    public void deleteInfo(Long id) {
        // 校验存在
        validateInfoExists(id);
        // 删除
        infoMapper.deleteById(id);
    }

    private void validateInfoExists(Long id) {
        if (infoMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public InfoDO getInfo(Long id) {
        return infoMapper.selectById(id);
    }

    @Override
    public PageResult<InfoDO> getInfoPage(InfoPageReqVO pageReqVO) {
        return infoMapper.selectPage(pageReqVO);
    }

}