package cn.iocoder.yudao.module.system.service.sys;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.BusinessConfigUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.BusinessParamsConfigDO;
import cn.iocoder.yudao.module.system.dal.mysql.sys.BusinessParamsConfigMapper;
import cn.iocoder.yudao.module.system.enums.NoticeConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.List;


/**
 * 系统业务参数配置 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class BusinessParamsConfigServiceImpl implements BusinessParamsConfigService {

    @Resource
    private BusinessParamsConfigMapper businessParamsConfigMapper;


    @Override
    public void updateBusinessParamsConfig(BusinessParamsConfigDO updateReqVO) {
        if (updateReqVO.getId() == null){
            businessParamsConfigMapper.insert(updateReqVO);
        }else {
            businessParamsConfigMapper.updateById(updateReqVO);
        }
    }

    @Override
    public List<BusinessParamsConfigDO> getList() {
        return businessParamsConfigMapper.selectList(null);
    }

    @Override
    public List<BusinessParamsConfigDO> getMonthlySettlementConfigByStatus(Integer value) {
        List<BusinessParamsConfigDO> businessParamsConfigList = businessParamsConfigMapper.selectList(new LambdaQueryWrapperX<BusinessParamsConfigDO>()
                .eq(BusinessParamsConfigDO::getEnableStatus, value)
                .eq(BusinessParamsConfigDO::getConfigCode, "EXPENSE_ORDER"));
        return businessParamsConfigList;
    }

    @Override
    public void batchUpdate(BusinessConfigUpdateReqVO updateReqVO) {
        if (NoticeConstants.BUSINESS_CONFIG_TITLE.equals(updateReqVO.getTitle())) {
            businessParamsConfigMapper.update(new LambdaUpdateWrapper<BusinessParamsConfigDO>()
                    .set(BusinessParamsConfigDO::getEnableStatus, updateReqVO.getEnableStatus())
                    .eq(BusinessParamsConfigDO::getTitle, updateReqVO.getTitle()));

            List<BusinessParamsConfigDO> updateList = updateReqVO.getUpdateList();
            updateList.forEach(businessParamsConfigDO -> {
                businessParamsConfigDO.setEnableStatus(null);
                businessParamsConfigDO.setTitle(null);
                businessParamsConfigDO.setConfigName(null);
                businessParamsConfigDO.setOrgType(null);
                businessParamsConfigMapper.updateById(businessParamsConfigDO);
            });
        }
    }
}