package cn.iocoder.yudao.module.pay.service.dividecompany;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.controller.admin.dividecompany.vo.DivideCompanyPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.dividecompany.vo.DivideCompanySaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.dividecompany.DivideCompanyDO;
import cn.iocoder.yudao.module.pay.dal.mysql.dividecompany.DivideCompanyMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.DIVIDE_COMPANY_NOT_EXISTS;

/**
 * 分账公司 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DivideCompanyServiceImpl implements DivideCompanyService {

    @Resource
    private DivideCompanyMapper divideCompanyMapper;

    @Override
    public Long createDivideCompany(DivideCompanySaveReqVO createReqVO) {
        // 插入
        DivideCompanyDO divideCompany = BeanUtils.toBean(createReqVO, DivideCompanyDO.class);
        divideCompanyMapper.insert(divideCompany);
        // 返回
        return divideCompany.getId();
    }

    @Override
    public void updateDivideCompany(DivideCompanySaveReqVO updateReqVO) {
        // 校验存在
        validateDivideCompanyExists(updateReqVO.getId());
        // 更新
        DivideCompanyDO updateObj = BeanUtils.toBean(updateReqVO, DivideCompanyDO.class);
        divideCompanyMapper.updateById(updateObj);
    }

    @Override
    public void deleteDivideCompany(Long id) {
        // 校验存在
        validateDivideCompanyExists(id);
        // 删除
        divideCompanyMapper.deleteById(id);
    }

    private void validateDivideCompanyExists(Long id) {
        if (divideCompanyMapper.selectById(id) == null) {
            throw exception(DIVIDE_COMPANY_NOT_EXISTS);
        }
    }

    @Override
    public DivideCompanyDO getDivideCompany(Long id) {
        return divideCompanyMapper.selectById(id);
    }

    @Override
    public PageResult<DivideCompanyDO> getDivideCompanyPage(DivideCompanyPageReqVO pageReqVO) {
        return divideCompanyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DivideCompanyDO> getList() {
        return divideCompanyMapper.selectList();
    }

    @Override
    public int computeAmount(String account, int totalPrice) {
        //todo:分账接收方被删除处理
        List<DivideCompanyDO> divideCompanyDOS = divideCompanyMapper.selectList(Wrappers.<DivideCompanyDO>lambdaQuery().eq(DivideCompanyDO::getAccount, account));
        DivideCompanyDO divideCompanyDO = divideCompanyDOS.get(0);
        return (int) Math.ceil(totalPrice * Double.parseDouble(divideCompanyDO.getProportion()));
    }
}