package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.AccountReportChangeMapper;
import com.crm.manager.service.AccountReportChangeService;
import com.crm.manager.service.SysConfigService;
import com.crm.manager.service.UserInfoService;
import com.crm.manager.service.task.AccountReportChangeFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.account.belong.AccountReportChangeDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.entity.account.belong.AccountReportChangeAddEntity;
import com.crm.rely.backend.core.entity.account.belong.AccountReportChangeListEntity;
import com.crm.rely.backend.core.enums.AccountReportChangeType;
import com.crm.rely.backend.core.pojo.table.AccountReportChangeTable;
import com.crm.rely.backend.core.pojo.table.UserInfoTable;
import com.crm.rely.backend.core.pojo.view.AccountReportChangeView;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AccountReportChangeServiceImpl implements AccountReportChangeService {

    @Autowired
    private AccountReportChangeMapper accountReportChangeMapper;
    @Autowired
    private AccountReportChangeFeignService accountReportChangeFeignService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UserInfoService userInfoService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AccountReportChangeAddEntity entity) throws ServiceException {
        AccountReportChangeTable table = new AccountReportChangeTable();
        if (!StringUtils.isEmpty(entity.getTypes())) {
            handleReport(entity);
        }
        BeanUtils.copyProperties(entity, table);
        accountReportChangeMapper.save(table);
    }

    void handleReport(AccountReportChangeAddEntity entity) throws ServiceException {

        try {
            Splitter splitter = Splitter.on(",");
            List<Integer> typeList = splitter.splitToList(entity.getTypes()).stream().map(Integer::parseInt).collect(Collectors.toList());
            //获取公司销售账号
            Long companySalesId = Long.parseLong(sysConfigService.getByCode(Constants.COMPANY_ACCOUNT_SUPER).getValue());
            entity.setFromSalesId(companySalesId);
            //转入销售信息
            UserInfoTable salesDbInfo = userInfoService.getDataById(entity.getToSalesId());
            entity.setIbNo(salesDbInfo.getIbNo());
            for (Integer type : typeList) {
                if (AccountReportChangeType.DW.getCode().equals(type)) {
                    accountReportChangeFeignService.handleDw(entity);
                } else if (AccountReportChangeType.SALES_COMMISSION.getCode().equals(type)) {
                    accountReportChangeFeignService.handleSalesCommission(entity);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }

    }

    @Override
    public AccountReportChangeDto getDetail(Long id) throws ServiceException {
        AccountReportChangeView view = accountReportChangeMapper.findOneView(id);
        return convertViewToDto(view);
    }

    @Override
    public BaseResultWithPagerDto<List<AccountReportChangeDto>> searchList(AccountReportChangeListEntity entity) throws ServiceException {

        Integer count = accountReportChangeMapper.countSearch(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count == null || count == 0) {
            return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
        }

        List<AccountReportChangeView> views = accountReportChangeMapper.listSearch(entity);
        List<AccountReportChangeDto> dtos = convertViewsToDtos(views);
        return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto, dtos);
    }

    private List<AccountReportChangeDto> convertViewsToDtos(List<AccountReportChangeView> views) {
        List<AccountReportChangeDto> dtos = Lists.newArrayList();
        for (AccountReportChangeView view : views) {
            AccountReportChangeDto dto = convertViewToDto(view);
            dtos.add(dto);

        }
        return dtos;
    }

    private AccountReportChangeDto convertViewToDto(AccountReportChangeView view) {
        AccountReportChangeDto dto = AccountReportChangeDto.getInstance();
        BeanUtils.copyProperties(view, dto);
        return dto;
    }
}
