package cn.superdesk.uniorder.dubbo.provider.unified.settlement;

import cn.superdesk.uniorder.common.enums.accounting.operator.SettlementRuleEnum;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.unified.settlement.UnifiedSettlementProgramme;
import cn.superdesk.uniorder.dto.param.operationCenter.customer.OperationCenterCustomerParam;
import cn.superdesk.uniorder.dto.param.operationCenter.customer.settlement.UnifiedSettlementParam;
import cn.superdesk.uniorder.dto.vo.operationCenter.customer.OperationCenterCustomerVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.customer.settlement.UnifiedSettlementVo;
import cn.superdesk.uniorder.dubbo.facade.unified.settlement.DubboUnifiedSettlementProgrammeService;
import cn.superdesk.uniorder.service.unified.settlement.UnifiedSettlementProgrammeService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

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

@Service(version = "1.0.1", retries = -1, timeout = 10000)
public class DubboUnifiedSettlementProgrammeServiceImpl implements DubboUnifiedSettlementProgrammeService {
    @Autowired
    UnifiedSettlementProgrammeService unifiedSettlementProgrammeService;
    @Override
    public UnifiedSettlementProgramme findById(Long id) {
        return unifiedSettlementProgrammeService.findById(id);
    }

    @Override
    public R<String> saveUnifiedSettlement(UnifiedSettlementParam param, Long sysUserId) {
        return unifiedSettlementProgrammeService.saveUnifiedSettlement(param, sysUserId);
    }

    @Override
    public R<String> updateUnifiedSettlement(UnifiedSettlementParam param, Long sysUserId) {
        return unifiedSettlementProgrammeService.updateUnifiedSettlement(param, sysUserId);
    }

    @Override
    public R<List<UnifiedSettlementVo>> list(OperationCenterCustomerParam param) {
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());

        List<UnifiedSettlementVo> list = unifiedSettlementProgrammeService.list(param);

       list= list.stream().map(vo->{
            vo.setSettlementRuleDesc(vo.getSettlementRule()!=null ? SettlementRuleEnum.getDescByKey(vo.getSettlementRule()) :"" );
            return vo;
        }).collect(Collectors.toList());

        PageInfo page = new PageInfo(list);

        return new R<List<UnifiedSettlementVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
    }

    @Override
    public R<UnifiedSettlementParam> get(Long id) {
        return unifiedSettlementProgrammeService.get(id);
    }

    @Override
    public R<String> delete(String ids,Long userId) {
        //unifiedSettlementProgrammeService.deleteObj(ids);

        List<Long> idsList = Arrays.stream(ids.split(",")).map(x -> Long.parseLong(x)).collect(Collectors.toList());
        unifiedSettlementProgrammeService.deleteByIds(idsList,userId);
        return new R<>("删除成功！");
    }

    @Override
    public R<String> enabled(Long id,Long userId) {

        UnifiedSettlementProgramme entity = unifiedSettlementProgrammeService.findById(id);
        if (null == entity) {
            return new R<>("500", "不存在该结算方案", false);
        }
        Integer enabled = entity.getEnabled();
        entity = new UnifiedSettlementProgramme();
        entity.setId(id);
        entity.setEnabled(enabled == 0 ? 1 : 0);
        entity.setUpdateUserId(userId);
        entity.setUpdateTime(new Date());
        unifiedSettlementProgrammeService.updateByPrimaryKeySelective(entity);
        String massage = entity.getEnabled() == 1 ? "启用成功！" : "停用成功！";
        return new R<>(massage);
    }

    @Override
    public R<String> setDefault(Long id,Long userId) {
        return unifiedSettlementProgrammeService.setDefault(id,userId);
    }

}
