package com.hotent.project.manager.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.query.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.exception.BaseException;
import com.hotent.base.util.StringUtil;
import com.hotent.baseInfo.manager.AuditedUnitManager;
import com.hotent.baseInfo.model.AuditedUnit;
import com.hotent.constant.BaseConstant;
import com.hotent.project.model.CostAnnualPlan;
import com.hotent.project.dao.CostAnnualPlanDao;
import com.hotent.project.manager.CostAnnualPlanManager;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.project.req.CostAnnualPlanPageReq;
import com.hotent.project.resp.CostAnnualPlanDetailResp;
import com.hotent.surveyinfo.model.CostSurveyFdTemplate;
import com.hotent.uc.api.model.IUser;
import com.hotent.uc.api.service.IOrgService;
import com.hotent.uc.manager.OrgManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.User;
import com.hotent.uc.util.ContextUtil;
import com.hotent.util.AreaCodeUtil;
import com.hotent.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import com.hotent.base.util.BeanUtils;


/**
 * 年度监审计划表 服务实现类
 *
 * @company 山西清众科技股份有限公司
 * @author 超级管理员
 * @since 2025-09-20
 */
@Service
public class CostAnnualPlanManagerImpl extends BaseManagerImpl<CostAnnualPlanDao, CostAnnualPlan> implements CostAnnualPlanManager {

	@Autowired
	private OrgManager orgManager;

	@Autowired
	private UserManager userService;

	@Autowired
	private AuditedUnitManager auditedUnitManager;

	@Override
	public CostAnnualPlanDetailResp getDetail(String id) {
    	CostAnnualPlan costAnnualPlan = this.get(id);

		CostAnnualPlanDetailResp detailResp = CopyUtil.copy(costAnnualPlan, CostAnnualPlanDetailResp.class);
		//单位级别：0:省/自治区/直辖市 1:市级 2:县级
		if(ObjectUtil.isNotEmpty(costAnnualPlan.getAreaCode()) && ObjectUtil.isNotEmpty(costAnnualPlan.getAreaLevel())){
			//如果是区县级
			if(BaseConstant.REGION_LEVEL_COUNTY.equals(costAnnualPlan.getAreaLevel())){
				String cityCode = AreaCodeUtil.getCityCode(costAnnualPlan.getAreaCode());
				String provinceCode = AreaCodeUtil.getProvinceCode(cityCode);
				detailResp.setCity(cityCode);
				detailResp.setProvince(provinceCode);
				detailResp.setCounty(costAnnualPlan.getAreaCode());
			}else if(BaseConstant.REGION_LEVEL_CITY.equals(costAnnualPlan.getAreaLevel())){
				String provinceCode = AreaCodeUtil.getProvinceCode(costAnnualPlan.getAreaCode());
				detailResp.setProvince(provinceCode);
				detailResp.setCity(costAnnualPlan.getAreaCode());
			}else if(BaseConstant.REGION_LEVEL_PROVINCE.equals(costAnnualPlan.getAreaLevel())){
				detailResp.setProvince(costAnnualPlan.getAreaCode());
			}else{
				detailResp.setProvince("未知编码");
			}
		}

		if(ObjectUtil.isNotEmpty(costAnnualPlan.getAuditedUnitId())){
			String[] split = costAnnualPlan.getAuditedUnitId().split(",");
			List<AuditedUnit> auditedUnits = auditedUnitManager.listByIds(Arrays.asList(split));
			detailResp.setAuditUnitName(auditedUnits.stream().map(AuditedUnit::getUnitName).collect(Collectors.joining(",")));
		}

		if(ObjectUtil.isNotEmpty(costAnnualPlan.getOrgId())){
			detailResp.setOrgName(orgManager.get(costAnnualPlan.getOrgId()).getName());
		}

		return detailResp;
	}
	@Override
	@Transactional
	public void createOrUpdate(CostAnnualPlan costAnnualPlan) {
		//新建或更新
		this.saveOrUpdate(costAnnualPlan);
	}

	@Override
	public PageList<CostAnnualPlan> pageList(CostAnnualPlanPageReq req) throws Exception {
		/*LambdaQueryWrapper<CostAnnualPlan> qw = new LambdaQueryWrapper<>();
		qw.eq(CostAnnualPlan::getIsDeleted, BaseConstant.NORMAL_STATUS);
		if (StringUtil.isNotEmpty(req.getProjectName())) {
			qw.like(CostAnnualPlan::getProjectName, req.getProjectName());
		}
		if(StringUtil.isNotEmpty(req.getPlanYear())){
			qw.eq(CostAnnualPlan::getPlanYear, req.getPlanYear());
		}
		if(StringUtil.isNotEmpty(req.getAreaCode())){
			qw.eq(CostAnnualPlan::getAreaCode,req.getAreaCode());
		}

		IPage<CostAnnualPlan> page = new Page<>(req.getPageNum(), req.getPageSize());
		return this.page(page,qw);*/

		IUser iUser = ContextUtil.getCurrentUser();
		User user = userService.getByAccount(iUser.getAccount());
		req.setDataScope(user.getDataScope());
		req.setProvinceCode(user.getProvinceCode());
		req.setCityCode(user.getCityCode());
		req.setCountyCode(user.getCountyCode());
		Page<CostAnnualPlan> objectPage = new Page<>(req.getPage(),req.getPageSize());
		//Page<CostSurveyTemplate> page =baseMapper.getCostSurveyTemplatePage(objectPage,params);
		Page<CostAnnualPlan> page =baseMapper.getCostAnnualPlanPage(objectPage
				,req.getDataScope()
				,req.getProvinceCode(),req.getCityCode()
				,req.getCountyCode()
				,req.getPlanYear()
				,req.getAreaCode()
				,req.getProjectName());
		return new PageList<>(page);
	}

	//新增
	@Override
	@Transactional
	public void create(CostAnnualPlan costAnnualPlan) {
		if(StringUtil.isEmpty(costAnnualPlan.getAreaLevel())){
			throw new BaseException("请选择地区级别");
		}
		costAnnualPlan.setIsDeleted(BaseConstant.NORMAL_STATUS);
		costAnnualPlan.setCreateTime(LocalDateTime.now());
		costAnnualPlan.setCreateBy(ContextUtil.getCurrentUser().getAccount());
		costAnnualPlan.setAreaLevel(AreaCodeUtil.getAreaLevel(costAnnualPlan.getAreaCode()).getLevel().getDescription());
		super.create(costAnnualPlan);
	}

	//修改
	@Override
	@Transactional
	public void update(CostAnnualPlan costAnnualPlan) {
		if(StringUtil.isEmpty(costAnnualPlan.getPlanId())){
			throw new BaseException("请选择要修改的记录");
		}
		if(StringUtil.isEmpty(costAnnualPlan.getAreaLevel())){
			throw new BaseException("请选择地区级别");
		}
		costAnnualPlan.setUpdateTime(LocalDateTime.now());
		costAnnualPlan.setUpdateBy(ContextUtil.getCurrentUser().getAccount());
		costAnnualPlan.setAreaLevel(AreaCodeUtil.getAreaLevel(costAnnualPlan.getAreaCode()).getLevel().getDescription());
		super.updateById(costAnnualPlan);
	}

	//删除
	@Transactional
	public void remove(String id) {
		if(StringUtil.isEmpty(id)){
			throw new BaseException("请选择要删除的记录");
		}
		CostAnnualPlan costAnnualPlan = new CostAnnualPlan();
		costAnnualPlan.setPlanId(id);
		costAnnualPlan.setIsDeleted(BaseConstant.DELETE_FLAG);
		super.updateById(costAnnualPlan);
	}

}
