package co.yixiang.yshop.module.mp.service.customerservice;

import co.yixiang.yshop.framework.common.pojo.PageParam;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.mp.controller.admin.customerservice.vo.CustomerServiceVO;
import co.yixiang.yshop.module.mp.controller.admin.customerservice.vo.WxCpKfAccountAddDTO;
import co.yixiang.yshop.module.mp.controller.admin.customerservice.vo.WxCpKfAccountUpdDTO;
import co.yixiang.yshop.module.mp.dal.dataobject.customerservice.CustomerServiceDO;
import co.yixiang.yshop.module.mp.dal.mysql.customerservice.CustomerServiceMapper;
import co.yixiang.yshop.module.mp.framework.cp.config.WxCpConfiguration;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpKfService;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.kf.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pepis
 * @apiNote
 **/
@Service
@Slf4j
public class CustomerServiceServiceImpl implements CustomerServiceService {
    @Resource
    private CustomerServiceMapper mapper;

    @Override
    public PageResult<CustomerServiceVO> page(PageParam pageParam) {
        WxCpKfService kfService = WxCpConfiguration.getCpService().getKfService();
        //客服列表
        PageResult<CustomerServiceDO> page = mapper.selectPage(pageParam, Wrappers.<CustomerServiceDO>lambdaQuery().orderByDesc(CustomerServiceDO::getId));
        List<CustomerServiceVO> collect = page.getList().stream().map(item -> {
            CustomerServiceVO customerServiceVO = new CustomerServiceVO();
            customerServiceVO.setName(item.getName());
            customerServiceVO.setAvatar(item.getAvatar());
            customerServiceVO.setOpenKfid(item.getOpenKfid());
            customerServiceVO.setAvatarUrl(item.getAvatarUrl());
            //客服链接
            WxCpKfAccountLink link = new WxCpKfAccountLink();
            link.setOpenKfid(item.getOpenKfid());
            try {
                WxCpKfAccountLinkResp accountLink = kfService.getAccountLink(link);
                customerServiceVO.setAccountLink(accountLink.getUrl());
            } catch (WxErrorException e) {
                log.warn("获取客服链接失败:{}", item.getOpenKfid());
                throw new RuntimeException(e);
            }
            return customerServiceVO;
        }).collect(Collectors.toList());

        PageResult<CustomerServiceVO> pageResult = PageResult.empty();
        pageResult.setList(collect);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    @SneakyThrows
    public WxCpKfAccountAddResp save(WxCpKfAccountAddDTO add) {
        WxCpKfService kfService = WxCpConfiguration.getCpService().getKfService();
        WxCpKfAccountAddResp wxCpKfAccountAddResp = kfService.addAccount(add);
        CustomerServiceDO customerServiceDO = new CustomerServiceDO();
        customerServiceDO.setName(add.getName());
        customerServiceDO.setAvatar(add.getMediaId());
        customerServiceDO.setOpenKfid(wxCpKfAccountAddResp.getOpenKfid());
        customerServiceDO.setAvatarUrl(add.getAvatarUrl());
        mapper.insert(customerServiceDO);
        return wxCpKfAccountAddResp;
    }

    @SneakyThrows
    @Override
    public void update(WxCpKfAccountUpdDTO upd) {
        WxCpKfService kfService = WxCpConfiguration.getCpService().getKfService();
        WxCpBaseResp resp = kfService.updAccount(upd);
        CustomerServiceDO customerServiceDO = new CustomerServiceDO();
        customerServiceDO.setName(upd.getName());
        customerServiceDO.setAvatar(upd.getMediaId());
        customerServiceDO.setOpenKfid(upd.getOpenKfid());
        customerServiceDO.setAvatarUrl(upd.getAvatarUrl());
        mapper.update(customerServiceDO, Wrappers.<CustomerServiceDO>lambdaUpdate().eq(CustomerServiceDO::getOpenKfid, upd.getOpenKfid()));
    }

    @SneakyThrows
    @Override
    public void delete(WxCpKfAccountDel del) {
        WxCpKfService kfService = WxCpConfiguration.getCpService().getKfService();
        WxCpBaseResp resp = kfService.delAccount(del);
        mapper.delete(Wrappers.<CustomerServiceDO>lambdaUpdate().eq(CustomerServiceDO::getOpenKfid, del.getOpenKfid()));
    }

    @Override
    public List<CustomerServiceDO> list() {
        return mapper.selectList();
    }
}
