package org.springblade.modules.pfInfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.byMonth.vo.ByMonthVO;
import org.springblade.modules.gzInfo.service.IInfoService;
import org.springblade.modules.gzInfo.vo.ZdVo;
import org.springblade.modules.gzInfo.vo.ZdVos;
import org.springblade.modules.gzInfo.vo.ZdVoss;
import org.springblade.modules.khsdPcs.service.IKhsdPcsService;
import org.springblade.modules.pfInfo.entity.PfInfoEntity;
import org.springblade.modules.pfInfo.excel.PfInfoExcel;
import org.springblade.modules.pfInfo.service.PfInfoService;
import org.springblade.modules.pfInfo.vo.PfInfoVO;
import org.springblade.modules.pfInfo.wrapper.PfInfoWrapper;
import org.springblade.modules.pfb.service.IPfbService;
import org.springblade.modules.sign.service.ISignService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.xpgxb.service.IXpgxbService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 手动打分 控制器
 *
 * @author BladeX
 * @since 2023-07-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("pfInfo/info")
@Api(value = "手动打分", tags = "手动打分接口")
public class PfInfoController extends BladeController {

	private final PfInfoService pfInfoService;

	private final IDictBizService dictBizService;

	private final ISignService signService;

	private final IKhsdPcsService khsdPcsService;

	private final IInfoService gzInfoService;

	private final IPfbService pfbService;

	private final IXpgxbService xpgxbService;

	/**
	 * 手动打分 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入info")
	public R<PfInfoVO> detail(PfInfoEntity info) {
		PfInfoEntity detail = pfInfoService.getOne(Condition.getQueryWrapper(info));
		return R.data(PfInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 手动打分 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<PfInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> info, Query query) {
		IPage<PfInfoEntity> pages = pfInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(info, PfInfoEntity.class));
		return R.data(PfInfoWrapper.build().pageVO(pages));
	}



	/**
	 * 手动打分 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<PfInfoVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<PfInfoVO> pages = pfInfoService.selectInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * app列表
	 */
	@GetMapping("/page-app")
	public R pageApp(@RequestParam Map<String, Object> params, Query query) {
		params.put("userId", AuthUtil.getUserId());
		IPage<PfInfoVO> pages = pfInfoService.pageApp(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 工作类型字典查询-WEB
	 */
	@GetMapping("/getPf")
	public R getPf(@RequestParam Map<String, Object> params) {
		List<ZdVo> yjzd = dictBizService.selectGzlx(0);
		Map<String, Object> temp = new HashMap<>();
		temp.put("sjhm", params.get("sjhm").toString());
		temp.put("yf", params.get("yf").toString().substring(0,7));
		ByMonthVO thisMonthPf = pfbService.getPf(temp);
		Date lastDate = DateUtil.parse(params.get("yf").toString().substring(0,7), "yyyy-MM");
		Calendar lastMonth = Calendar.getInstance();
		lastMonth.setTime(lastDate);
		lastMonth.add(Calendar.MONTH, -1); // 上个月
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		temp.put("yf", sdf.format(lastMonth.getTime()));
		ByMonthVO lastMonthPf = pfbService.getPf(temp);
		if (null == thisMonthPf) {
			thisMonthPf = new ByMonthVO();
			thisMonthPf.setDjgzFs("0");
			thisMonthPf.setCqkqFs("0");
			thisMonthPf.setGzjlFs("0");
		}
		if (null == lastMonthPf) {
			lastMonthPf = new ByMonthVO();
			lastMonthPf.setDjgzFs("10");
			lastMonthPf.setCqkqFs("10");
			lastMonthPf.setGzjlFs("10");
		}
		for (ZdVo yj : yjzd) {
			List<ZdVos> ejzd = dictBizService.selectGzlxs(yj.getId(), 0);
			for (ZdVos ej : ejzd) {
				List<ZdVoss> sjzd = dictBizService.selectGzlxss(ej.getId(), 0);
				for (ZdVoss sj : sjzd) {
					// 所有的查询sql需要加sjhm和userId和yf
					int time = 0;
					Integer khcs = 0;
					// 出勤考勤
					if (sj.getLabel().equals("打卡异常")) {
						// 旷工次数
						params.put("type", 3);
						time = signService.getKgCs(params);
						// 一日减0.5
						sj.setXtkf(-time * 0.5);
					} else if (sj.getLabel().equals("请假")) {
						// 请假次数
						params.put("type", 4);
						time = signService.getKgCs(params);
						// 一日减0.2
						sj.setXtkf(Double.valueOf(new DecimalFormat("#.00").format((-time * 0.2))));
					}
					// 党建工作
					if (sj.getLabel().equals("党支部活动") || sj.getLabel().equals("四联四促活动")) {
						params.put("pfxId", sj.getId());
						// 规定需要参加的次数
						khcs = khsdPcsService.getKhcs(params);
						if (null == khcs) {
							khcs = 0;
						}
						// 实际参加次数
						params.put("pfx", sj.getLabel());
						time = gzInfoService.getDzbhdCs(params);
						// 少参加一次减0.5
						sj.setXtkf(time >= khcs ? 0 : (-(khcs - time) * 0.5));
					}
					// 工作纪律
					if (sj.getLabel().equals("不服从派出所工作安排和管理") || sj.getLabel().equals("群众有理投诉或被上级部门通报")) {
						// 实际参加次数
						params.put("pfxId", sj.getId());
						time = pfInfoService.getDzbhdCs(params);
						// 少参加一次减0.5
						sj.setXtkf(-time * 0.5);
					}
					// 系统扣分
					ej.setXtkf(ej.getXtkf() + sj.getXtkf());
					if (ej.getLabel().equals("党建工作")) {
						ej.setDwpf(Double.parseDouble(lastMonthPf.getDjgzFs()) + ej.getXtkf());
						ej.setSydf(lastMonthPf.getDjgzFs());
					} else if (ej.getLabel().equals("出勤考勤")) {
						ej.setDwpf(Double.parseDouble(lastMonthPf.getCqkqFs()) + ej.getXtkf());
						ej.setSydf(lastMonthPf.getCqkqFs());
					} else if (ej.getLabel().equals("工作纪律")) {
						ej.setDwpf(Double.parseDouble(lastMonthPf.getGzjlFs()) + ej.getXtkf());
						ej.setSydf(lastMonthPf.getGzjlFs());
					}
				}
				ej.setChildren(sjzd);
			}
			yj.setChildren(ejzd);
		}
		return R.data(yjzd);
	}

	/**
	 * 手动打分 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入info")
	public R save(@Valid @RequestBody PfInfoEntity info) {
		info.setYf(info.getYf().substring(0, 7));
		return R.status(pfInfoService.save(info));
	}

	/**
	 * 监督检查-新增-APP
	 */
	@GetMapping("/saveBatch")
	public R saveBatch(@RequestParam Map<String, Object> params) {
		String[] sjhms = params.get("sjhms").toString().split(",");
		List<PfInfoEntity> PfInfoList = new ArrayList<>();
		for (String sjhm : sjhms) {
			PfInfoEntity pfInfo = new PfInfoEntity();
			pfInfo.setSjhm(sjhm);
			pfInfo.setYf(params.get("yf").toString());
			pfInfo.setPfxId(Long.valueOf(params.get("pfxId").toString()));
			pfInfo.setKfyy(params.get("kfyy").toString());
			if(params.get("file")!=null || params.get("file") !=""){
				pfInfo.setFile(params.get("file").toString());
			}
			PfInfoList.add(pfInfo);
		}
		return R.status(pfInfoService.saveBatch(PfInfoList));
	}

	/**
	 * 手动打分 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R update(@Valid @RequestBody PfInfoEntity info) {
		return R.status(pfInfoService.updateById(info));
	}

	/**
	 * 手动打分 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入info")
	public R submit(@Valid @RequestBody PfInfoEntity info) {
		return R.status(pfInfoService.saveOrUpdate(info));
	}

	/**
	 * 手动打分 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(pfInfoService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-info")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入info")
	public void exportInfo(@ApiIgnore @RequestParam Map<String, Object> info, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<PfInfoEntity> queryWrapper = Condition.getQueryWrapper(info, PfInfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Info::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(PfInfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<PfInfoExcel> list = pfInfoService.exportInfo(queryWrapper);
		ExcelUtil.export(response, "手动打分数据" + DateUtil.time(), "手动打分数据表", list, PfInfoExcel.class);
	}

}
