package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SysSalesShareMapper;
import com.crm.manager.dao.mapper.UserInfoDao;
import com.crm.manager.dao.repository.UserInfoRepository;
import com.crm.manager.service.SysSalesShareService;
import com.crm.manager.service.UserRoleService;
import com.crm.manager.service.task.TradeSaleCommissionFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.system.sales.SysSalesShareDto;
import com.crm.rely.backend.core.entity.system.sales.SysSalesShareListEntity;
import com.crm.rely.backend.core.entity.user.info.UserShareUpdateEntity;
import com.crm.rely.backend.core.pojo.table.SysSalesShareTable;
import com.crm.rely.backend.core.pojo.table.UserInfoTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ddf.UnknownEscherRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author y
 * @create 2022/6/10
 */
@Service
public class SysSalesShareServiceImpl implements SysSalesShareService {

    @Autowired
    private SysSalesShareMapper sysSalesShareMapper;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private TradeSaleCommissionFeignService tradeSaleCommissionFeignService;

    @Override
    public void add(UserShareUpdateEntity entity) throws Exception {

        Date now = new Date();

        //需修改的用户信息
        UserInfoTable user = userInfoDao.findFirstByIbNo(entity.getSalesNo());
        if (user == null) {
            throw new ServiceException("销售或代理不存在");
        }
        //根据编号获取所有的销售信息
        List<UserInfoTable> sales = userInfoDao.findAllAndSelfByStamp(user.getStamp());
        if (CollectionUtils.isEmpty(sales)) return;

        //当前用户信息
        UserInfoTable currentUser = userInfoRepository.findOne(entity.getUserId());
        if (currentUser == null) {
            throw new ServiceException("system_error");
        }
        /*
            验证是否有权限
         */
        List<SysSalesShareTable> tables = Lists.newArrayList();
        for (UserInfoTable sale : sales) {
            String saleStamp = sale.getStamp();
            if (StringUtils.isEmpty(saleStamp)) {
                throw new ServiceException("system_error");
            }
            if (!saleStamp.startsWith(currentUser.getStamp())) {
                throw new ServiceException("not_permit");
            }
            SysSalesShareTable table = new SysSalesShareTable();
            table.setSalesId(sale.getId());
            table.setSalesNo(sale.getIbNo());
            table.setOldShare(sale.getSalesShare());
            table.setNewShare(entity.getShare());
            table.setAddTime(now);
            table.setAddUser(entity.getUserId());
            tables.add(table);

        }
        //更新信息
        userInfoDao.updateShareByStamp(entity.getShare(), user.getStamp());
        //保存记录
        if (!CollectionUtils.isEmpty(tables)) {
            sysSalesShareMapper.batchSave(tables);
        }
        //更新当月已经计算的返佣信息
        BaseResultDto baseResultDto = tradeSaleCommissionFeignService.reCalc(entity);
        if (Constants.SUCCESS_CODE != baseResultDto.getCode()) {
            throw new ServiceException(baseResultDto.getMsg());
        }

    }

    @Override
    public BaseResultWithPagerDto<List<SysSalesShareDto>> searchList(SysSalesShareListEntity entity) throws Exception {

        Integer count = sysSalesShareMapper.countSearch(entity);
        if (count == null || count == 0) {
            return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, null);
        }

        PageDto pageDto = FormatPage.format(entity.getPage(), count);

        List<SysSalesShareDto> dtos = Lists.newArrayList();
        List<SysSalesShareTable> tables = sysSalesShareMapper.listSearch(entity);
        for (SysSalesShareTable table : tables) {
            SysSalesShareDto dto = new SysSalesShareDto();
            BeanUtils.copyProperties(table, dto);
            dtos.add(dto);
        }
        return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto, dtos);
    }
}
