package vip.xiaonuo.production.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.erp.entity.SysBaseFilterInfo;
import vip.xiaonuo.erp.mapper.SysBaseFilterInfoMapper;
import vip.xiaonuo.production.entity.ProductionWritePlan;
import vip.xiaonuo.production.mapper.ProductionWritePlanMapper;
import vip.xiaonuo.production.param.ProductionPageParam;
import vip.xiaonuo.production.param.ProductionSaveParam;
import vip.xiaonuo.production.param.ProductionWriteListParam;
import vip.xiaonuo.production.param.ProductionWritePlanAddParam;
import vip.xiaonuo.production.service.IProductionWritePlanService;
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.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 计划生产量填报 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-25
 */
@Service
public class ProductionWritePlanServiceImpl extends ServiceImpl<ProductionWritePlanMapper, ProductionWritePlan> implements IProductionWritePlanService {

    @Autowired
    private ProductionWritePlanMapper planMapper;
    @Autowired
    private SysOrgMapper orgMapper;
    @Autowired
    private SysBaseFilterInfoMapper filterInfoMapper;

    @Override
    public CommonResult add(ProductionWritePlanAddParam writePlan) throws ParseException {
        String orgId = writePlan.getOrgId();
        SysOrg org = orgMapper.selectOne(new QueryWrapper<SysOrg>().eq("id", orgId));
        ProductionWritePlan plan = new ProductionWritePlan();
        BeanUtil.copyProperties(writePlan, plan);
        plan.setOrgName(org.getName());
        planMapper.insert(plan);
        return CommonResult.ok();
    }

    @Override
    public CommonResult list(Map<String, String> paramsMap) {
        return null;
    }

    @Override
    public CommonResult edit(ProductionWritePlan writePlan) {
        planMapper.updateById(writePlan);
        return CommonResult.ok();
    }

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

    @Override
    public Page<ProductionWritePlan> page(ProductionPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        QueryWrapper<ProductionWritePlan> queryWrapper = new QueryWrapper();
        Page<ProductionWritePlan> page = new Page<>(pageNum, pageSize);
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        return planMapper.selectPage(page, queryWrapper);
    }

    @Override
    public CommonResult save(ProductionSaveParam saveParam) {
        Integer isErp = saveParam.getIsErp();
        String orgId = saveParam.getOrgId();
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        QueryWrapper<SysBaseFilterInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getId()).eq("module_type", "produce");
        if ("".equals(orgId) || null == orgId) {
            queryWrapper.eq("org_id", "0");
        } else {
            queryWrapper.eq("org_id",orgId);
        }
        SysBaseFilterInfo sysBaseFilterInfo = filterInfoMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(sysBaseFilterInfo)) {
            SysBaseFilterInfo info = new SysBaseFilterInfo();
            info.setModuleType("produce");
            info.setUserId(loginUser.getId());
            info.setModuleType(isErp.toString());
            info.setOrgId("0");
            filterInfoMapper.insert(info);
        } else {
            sysBaseFilterInfo.setFilter(isErp.toString());
            filterInfoMapper.updateById(sysBaseFilterInfo);
        }
        return CommonResult.ok();
    }

    @Override
    public SysBaseFilterInfo getOne(ProductionWriteListParam writeListParam) {
        String orgId = writeListParam.getOrgId();
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        QueryWrapper<SysBaseFilterInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUser.getId()).eq("module_type", "produce");
        if ("".equals(orgId) || null == orgId) {
            queryWrapper.eq("org_id", "0");
        } else {
            queryWrapper.eq("org_id", orgId);
        }
        SysBaseFilterInfo sysBaseFilterInfo = filterInfoMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(sysBaseFilterInfo)){
            sysBaseFilterInfo = new SysBaseFilterInfo();
            sysBaseFilterInfo.setUserId(loginUser.getId());
            sysBaseFilterInfo.setFilter("0");
            sysBaseFilterInfo.setOrgId("0");
            sysBaseFilterInfo.setModuleType("produce");
            filterInfoMapper.insert(sysBaseFilterInfo);
            return sysBaseFilterInfo;
        }
        return sysBaseFilterInfo;
    }

}
