package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.MamPmPercentChangeMapper;
import com.crm.manager.dao.repository.MamListRepository;
import com.crm.manager.dao.repository.MamPmPercentChangeRepository;
import com.crm.manager.service.MamPmPercentChangeService;
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.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.mam.MamPmPercentChangeDto;
import com.crm.rely.backend.core.entity.base.BaseApproveEntity;
import com.crm.rely.backend.core.entity.mam.MamPmPercentChangeListEntity;
import com.crm.rely.backend.core.enums.CommonStatusType;
import com.crm.rely.backend.core.pojo.table.MamListTable;
import com.crm.rely.backend.core.pojo.table.MamPmPercentChangeTable;
import com.crm.rely.backend.exception.ServiceException;
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 java.util.List;

/**
 * pm业务
 *
 * @author y
 * @create 2022/1/21
 */
@Service
public class MamPmPercentChangeServiceImpl implements MamPmPercentChangeService {

    @Autowired
    private MamPmPercentChangeMapper mamPmPercentChangeMapper;
    @Autowired
    private MamListRepository mamListRepository;
    @Autowired
    private MamPmPercentChangeRepository mamPmPercentChangeRepository;


    @Override
    public BaseResultWithPagerDto<List<MamPmPercentChangeDto>> getList(MamPmPercentChangeListEntity entity) throws Exception {

        Integer count = mamPmPercentChangeMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }

        List<MamPmPercentChangeTable> tables = mamPmPercentChangeMapper.getList(entity);
        List<MamPmPercentChangeDto> dtos = Lists.newArrayList();
        for (MamPmPercentChangeTable table : tables) {
            dtos.add(convertTableToDto(table));
        }
        return new BaseResultWithPagerDto(pageDto, dtos);
    }

    @Override
    public MamPmPercentChangeDto getDetail(Long id) throws Exception {

        MamPmPercentChangeTable table = mamPmPercentChangeRepository.getOne(id);
        if (table == null) {
            throw new ServiceException("info_not_found");
        }
        return convertTableToDto(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean approve(BaseApproveEntity entity) throws Exception {

        MamPmPercentChangeTable table = mamPmPercentChangeRepository.getOne(entity.getId());
        if (table == null) {
            throw new ServiceException("info_not_found");
        }

        BeanUtils.copyProperties(entity, table);
        mamPmPercentChangeRepository.save(table);
        /*
            审批通过修改比例
         */
        if (CommonStatusType.PASS.getCode() == entity.getStatus()){
            MamListTable mamListTable = mamListRepository.getOne(table.getMamListId());
            if (mamListTable == null) {
                throw new ServiceException("info_not_found");
            }
            mamListTable.setPercent(table.getNewPercent());
            mamListTable.setModifyTime(entity.getApproveTime());
            mamListTable.setModifyUser(entity.getApproveUser());
            mamListTable.setModifyIp(entity.getModifyIp());
            mamListRepository.save(mamListTable);
        }

        return true;
    }

    private MamPmPercentChangeDto convertTableToDto(MamPmPercentChangeTable table) throws Exception {
        MamPmPercentChangeDto dto = new MamPmPercentChangeDto();
        BeanUtils.copyProperties(table, dto);
        dto.setStatusName(CommonStatusType.matchCode(table.getStatus()).getDesc());
        return dto;
    }
}
