package vip.xiaonuo.money.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.money.entity.MoneyCost;
import vip.xiaonuo.money.entity.MoneyProducePrice;
import vip.xiaonuo.money.mapper.MoneyCostMapper;
import vip.xiaonuo.money.param.MoneyInOutPageParam;
import vip.xiaonuo.money.param.MoneyInOutParam;
import vip.xiaonuo.money.service.IMoneyCostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.mapper.SysOrgMapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 财务费用分析 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class MoneyCostServiceImpl extends ServiceImpl<MoneyCostMapper, MoneyCost> implements IMoneyCostService {

    @Autowired
    private MoneyCostMapper costMapper;
    @Autowired
    private SysOrgMapper orgMapper;

    @Override
    public List<MoneyCost> listCost(MoneyInOutParam outParam) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.MONTH, -11);
        String currentDateString = sdf.format(calendar.getTime());
        QueryWrapper<MoneyCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time", currentDateString);
        queryWrapper.eq(StringUtils.isNotBlank(outParam.getOrgId()), "org_id", outParam.getOrgId());
        queryWrapper.select(" IFNULL(sum(manage),'0') manage,IFNULL(sum(sale),'0') sale,IFNULL(sum(finance),'0') finance," +
                "create_time createTime");
        queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        queryWrapper.orderByAsc("create_time");
        return costMapper.selectList(queryWrapper);

    }

    @Override
    public CommonResult add(MoneyCost moneyCost) {
        String orgId = moneyCost.getOrgId();
        if ("".equals(orgId) || null == orgId) {
            return CommonResult.error("公司不能为空");
        }
        SysOrg sysOrg = orgMapper.selectById(orgId);
        moneyCost.setCompany(sysOrg.getName());
        costMapper.insert(moneyCost);
        return CommonResult.ok();
    }

    @Override
    public CommonResult update(MoneyCost moneyCost) {
        costMapper.updateById(moneyCost);
        return CommonResult.ok();
    }

    @Override
    public CommonResult delete(Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        costMapper.deleteById(id);
        return CommonResult.ok();
    }

    @Override
    public CommonResult page(MoneyInOutPageParam pageParam) {
        QueryWrapper<MoneyCost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(pageParam.getOrgId()), "org_id", pageParam.getOrgId());
        Page<MoneyCost> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        Page<MoneyCost> page1 = costMapper.selectPage(page, queryWrapper);
        List<MoneyCost> records = page1.getRecords();
        for (MoneyCost record : records) {
            record.setFinance(record.getFinance());
            record.setManage(record.getManage());
            record.setSale(record.getSale());
        }
        return CommonResult.data(page1);
    }

}
