package com.fs.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.company.service.ICompanyTrafficRecordService;
import com.fs.system.domain.SysConfig;
import com.fs.system.mapper.SysConfigMapper;
import com.fs.system.service.ISysDeptConfigLogService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.system.mapper.SysDeptConfigMapper;
import com.fs.system.domain.SysDeptConfig;
import com.fs.system.service.ISysDeptConfigService;

/**
 * 部门配置Service业务层处理
 * 
 * @author fs
 * @date 2025-09-22
 */
@Service
@AllArgsConstructor
public class SysDeptConfigServiceImpl extends ServiceImpl<SysDeptConfigMapper, SysDeptConfig> implements ISysDeptConfigService {


    private final ISysDeptConfigLogService sysDeptConfigLogService;
    private final SysConfigMapper sysConfigMapper;

    /**
     * 查询部门配置
     * 
     * @param id 部门配置主键
     * @return 部门配置
     */
    @Override
    public SysDeptConfig selectSysDeptConfigById(Long id)
    {
        return baseMapper.selectSysDeptConfigById(id);
    }

    /**
     * 查询部门配置列表
     * 
     * @param sysDeptConfig 部门配置
     * @return 部门配置
     */
    @Override
    public List<SysDeptConfig> selectSysDeptConfigList(SysDeptConfig sysDeptConfig)
    {
        return baseMapper.selectSysDeptConfigList(sysDeptConfig);
    }

    /**
     * 新增部门配置
     * 
     * @param sysDeptConfig 部门配置
     * @return 结果
     */
    @Override
    public int insertSysDeptConfig(SysDeptConfig sysDeptConfig)
    {
        sysDeptConfig.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertSysDeptConfig(sysDeptConfig);
    }

    /**
     * 修改部门配置
     * 
     * @param sysDeptConfig 部门配置
     * @return 结果
     */
    @Override
    public int updateSysDeptConfig(SysDeptConfig sysDeptConfig)
    {
        sysDeptConfig.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateSysDeptConfig(sysDeptConfig);
    }

    /**
     * 批量删除部门配置
     * 
     * @param ids 需要删除的部门配置主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptConfigByIds(Long[] ids)
    {
        return baseMapper.deleteSysDeptConfigByIds(ids);
    }
    /**
     * 删除部门配置信息
     * 
     * @param id 部门配置主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptConfigById(Long id)
    {
        return baseMapper.deleteSysDeptConfigById(id);
    }

    @Override
    public void updatePadNum(Long deptId, Integer num, LoginUser loginUser, LocalDate endTime) {
        SysDeptConfig deptConfig = getDeptConfig(deptId, loginUser);
        deptConfig.setUpdateBy(loginUser.getUsername());
        deptConfig.setUpdateTime(new Date());
        Long padNum = deptConfig.getPadNum() == null ? 0L : deptConfig.getPadNum();
        deptConfig.setPadNum(num.longValue());
        long n = num - padNum;
        deptConfig.setPadTime(endTime);
        sysDeptConfigLogService.addLog(deptId, 2, n >= 0 ? 0 : 1, num.toString(), padNum.toString(), deptConfig.getPadNum().toString(), loginUser.getUsername(), "");
        updateById(deptConfig);
    }

    @Override
    public void addRedMoney(Long deptId, BigDecimal num, LoginUser loginUser) {
        SysDeptConfig deptConfig = getDeptConfig(deptId, loginUser);
        deptConfig.setUpdateBy(loginUser.getUsername());
        deptConfig.setUpdateTime(new Date());
        BigDecimal red = deptConfig.getRedPackage() == null ? BigDecimal.ZERO : deptConfig.getRedPackage();
        deptConfig.setRedPackage(red.add(num));
        sysDeptConfigLogService.addLog(deptId, 0, 0, num.setScale(2, RoundingMode.UP).toString(), red.setScale(2, RoundingMode.UP).toString(), deptConfig.getRedPackage().setScale(2, RoundingMode.UP).toString(), loginUser.getUsername(), "");
        updateById(deptConfig);

    }

    @Override
    public void addFlowNum(Long deptId, BigDecimal money, LoginUser loginUser) {
        SysDeptConfig deptConfig = getDeptConfig(deptId, loginUser);
        deptConfig.setUpdateBy(loginUser.getUsername());
        deptConfig.setUpdateTime(new Date());
        Long l = trafficConversion(money.longValue());
        long flowNum = deptConfig.getFlowNum() == null ? 0 : deptConfig.getFlowNum();
        deptConfig.setFlowNum(flowNum + l);
        sysDeptConfigLogService.addLog(deptId, 1, 0, l.toString(), flowNum + "", deptConfig.getFlowNum().toString(), loginUser.getUsername(), "");
        updateById(deptConfig);

    }
    public Long trafficConversion(Long amount) {
        SysConfig config = sysConfigMapper.selectConfigByConfigKey("statis.config");
        JSONObject jsonObject = JSONObject.parseObject(config.getConfigValue());
        float trafficPrice = jsonObject.getFloatValue("trafficPrice");
        double trafficGB = amount / trafficPrice;
        return (long) (trafficGB * 1024 * 1024);
    }
    @Override
    public SysDeptConfig getDeptConfig(Long deptId, LoginUser loginUser){
        SysDeptConfig one = getDeptConfig(deptId);
        if(one == null){
            one = new SysDeptConfig();
            one.setDeptId(deptId);
            one.setCreateBy(loginUser.getUsername());
            one.setCreateTime(new Date());
            save(one);
        }
        return one;
    }

    @Override
    public SysDeptConfig getDeptConfig(Long deptId){
        return getOne(new QueryWrapper<SysDeptConfig>().eq("dept_id", deptId));
    }
}
