package cn.iocoder.yudao.module.crm.service.crmcustomercontacts;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomercontacts.vo.CrmCustomerContactsPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomercontacts.vo.CrmCustomerContactsRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.crmcustomercontacts.vo.CrmCustomerContactsSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmcustomer.CrmCustomerDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.crmcustomercontacts.CrmCustomerContactsDO;
import cn.iocoder.yudao.module.crm.dal.mysql.crmcustomer.CrmCustomerMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.crmcustomercontacts.CrmCustomerContactsMapper;
import cn.iocoder.yudao.module.crm.service.userlivetree.UserLiveTreeService;
import cn.iocoder.yudao.module.hrm.enums.RelationEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.CUSTOMER_CONTACTS_NOT_EXISTS;

/**
 * 联系人 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class CrmCustomerContactsServiceImpl implements CrmCustomerContactsService {

    @Resource
    private CrmCustomerContactsMapper customerContactsMapper;
    @Resource
    private CrmCustomerMapper customerMapper;
    @Resource
    private UserLiveTreeService userLiveTreeService;

    @Override
    public Long createCustomerContacts(CrmCustomerContactsSaveReqVO createReqVO) {
        // 插入
        CrmCustomerContactsDO customerContacts = BeanUtils.toBean(createReqVO, CrmCustomerContactsDO.class);
        customerContacts.setOwnerUserId(SecurityFrameworkUtils.getLoginUserId());
        customerContactsMapper.insert(customerContacts);
        // 返回
        return customerContacts.getId();
    }

    @Override
    public void updateCustomerContacts(CrmCustomerContactsSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerContactsExists(updateReqVO.getId());
        // 更新
        CrmCustomerContactsDO updateObj = BeanUtils.toBean(updateReqVO, CrmCustomerContactsDO.class);
        customerContactsMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerContacts(Long id) {
        // 校验存在
        validateCustomerContactsExists(id);
        // 删除
        customerContactsMapper.deleteById(id);
    }

    private void validateCustomerContactsExists(Long id) {
        if (customerContactsMapper.selectById(id) == null) {
            throw exception(CUSTOMER_CONTACTS_NOT_EXISTS);
        }
    }

    @Override
    public CrmCustomerContactsRespVO getCustomerContacts(Long id) {
        CrmCustomerContactsDO crmCustomerContactsDO = customerContactsMapper.selectById(id);
        CrmCustomerContactsRespVO contactsRespVO = BeanUtils.toBean(crmCustomerContactsDO, CrmCustomerContactsRespVO.class);
        CrmCustomerDO crmCustomerDO = customerMapper.selectById(crmCustomerContactsDO.getCustomerId());
        contactsRespVO.setCustomerName(crmCustomerDO.getName());
        return contactsRespVO;
    }

    @Override
    public PageResult<CrmCustomerContactsRespVO> getCustomerContactsPage(CrmCustomerContactsPageReqVO pageReqVO) {
        List<Long> ids = new ArrayList<>();
        Long adminId = SecurityFrameworkUtils.getLoginUserId();
        if (RelationEnum.MY.getValue().equals(pageReqVO.getRelation())) {
            ids.add(adminId);
        } else if (RelationEnum.SUB.getValue().equals(pageReqVO.getRelation())) {
            ids = userLiveTreeService.getItemIdsByUserId(adminId);
        }
        IPage mpPage = MyBatisUtils.buildPage(pageReqVO);
        IPage<CrmCustomerContactsRespVO> pageResult = customerContactsMapper.selectPageList(mpPage, pageReqVO, ids);
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }
}
