package org.springblade.modules.sqmy.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 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.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.rhzf.fwInfo.service.IFwInfoService;
import org.springblade.modules.sqmy.excel.SqmyInfoExcel;
import org.springblade.modules.sqmy.pojo.entity.*;
import org.springblade.modules.sqmy.pojo.vo.SqmyInfoVO;
import org.springblade.modules.sqmy.pojo.vo.SqmyTjVO;
import org.springblade.modules.sqmy.service.*;
import org.springblade.modules.sqmy.wrapper.SqmyInfoWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 社情民意 控制器
 *
 * @author wth
 * @since 2024-11-12
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sqmyInfo")
@Api(value = "社情民意", tags = "社情民意接口")
public class SqmyInfoController extends BladeController {

	private final ISqmyInfoService sqmyInfoService;

	private final ISqmyRyService sqmyRyService;

	private final IFwInfoService fwInfoService;

	private final IJmhInfoService jmhInfoService;

	private final IFzrhyInfoService fzrhyInfoService;

	private final IZatbInfoService zatbInfoService;

	/**
	 * 社情民意 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sqmyInfo")
	public R<SqmyInfoVO> detail(SqmyInfoEntity sqmyInfo) {
		// 查询社情民意信息
		SqmyInfoEntity detail = sqmyInfoService.getOne(Condition.getQueryWrapper(sqmyInfo));
		SqmyInfoVO sqmyInfoVO = new SqmyInfoVO();
		BeanUtils.copyProperties(detail, sqmyInfoVO);
		// 查询人员信息
		QueryWrapper<SqmyRyEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sqmy_id", sqmyInfo.getId());
		List<SqmyRyEntity> ryList = sqmyRyService.list(queryWrapper);
		sqmyInfoVO.setRyList(ryList);
		return R.data(sqmyInfoVO);
	}

	/**
	 * 社情民意 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sqmyInfo")
	public R<IPage<SqmyInfoVO>> list(@RequestParam Map<String, Object> sqmyInfo, Query query) {
		IPage<SqmyInfoEntity> pages = sqmyInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(sqmyInfo, SqmyInfoEntity.class));
		return R.data(SqmyInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 社情民意 数据统计
	 */
	@GetMapping("/sjtj")
	public R sjtj(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		SqmyTjVO sqmyTjVO = new SqmyTjVO();
		// 年度社情民意
		int ndSqmy = sqmyInfoService.countByType(1, params);
		sqmyTjVO.setNdSqmy(ndSqmy);
		// 季度社情民意
		int jdSqmy = sqmyInfoService.countByType(2, params);
		sqmyTjVO.setJdSqmy(jdSqmy);
		// 本日社情民意
		int brSqmy = sqmyInfoService.countByType(3, params);
		sqmyTjVO.setBrSqmy(brSqmy);
		// 本年度入户走访户数
		int ndRhzf = fwInfoService.countByType(1, params);
		sqmyTjVO.setNdRhzf(ndRhzf);
		// 本周入户走访户数
		int bzRhzf = fwInfoService.countByType(2, params);
		sqmyTjVO.setBzRhzf(bzRhzf);
		// 获取今天的日期
		LocalDate today = LocalDate.now();
		// 获取今天是今年的第几周
		int weekOfYear = today.get(WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear());
		sqmyTjVO.setWeekOfYear(weekOfYear);
		// 获取今天是今年的第几季度
		int quarterOfYear = (today.getMonthValue() - 1) / 3 + 1;
		sqmyTjVO.setQuarterOfYear(quarterOfYear);
		// 获取今天是今年的第几月
		int monthOfYear = today.getMonthValue();
		sqmyTjVO.setMonthOfYear(monthOfYear);
		// 本周见面会情况
		JmhInfoEntity jhm = jmhInfoService.getBzJmh(params);
		sqmyTjVO.setJhm(jhm);
		// 本月安全负责人会议
		FzrhyInfoEntity fzrhy = fzrhyInfoService.getByFzrhy(params);
		sqmyTjVO.setFzrhy(fzrhy);
		// 本季度治安通报情况
		ZatbInfoEntity zatb = zatbInfoService.getBjdZatb(params);
		sqmyTjVO.setZatb(zatb);
		return R.data(sqmyTjVO);
	}

	/**
	 * 社情民意 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sqmyInfo")
	public R<IPage<SqmyInfoVO>> page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<SqmyInfoVO> pages = sqmyInfoService.selectSqmyInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 社情民意 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sqmyInfo")
	public R save(@Valid @RequestBody SqmyInfoVO sqmyInfo) {
		sqmyInfoService.save(sqmyInfo);
		if (null != sqmyInfo.getRyList() && sqmyInfo.getRyList().size() > 0) {
			for (SqmyRyEntity sqmyRyEntity : sqmyInfo.getRyList()) {
				sqmyRyEntity.setSqmyId(sqmyInfo.getId());
				sqmyRyService.save(sqmyRyEntity);
			}
		}
		return R.status(true);
	}

	/**
	 * 社情民意 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sqmyInfo")
	public R update(@Valid @RequestBody SqmyInfoVO sqmyInfo) {
		sqmyInfoService.updateById(sqmyInfo);
		if (null != sqmyInfo.getRyList() && sqmyInfo.getRyList().size() > 0) {
			for (SqmyRyEntity sqmyRyEntity : sqmyInfo.getRyList()) {
				if (null != sqmyRyEntity.getIsDeleted() && sqmyRyEntity.getIsDeleted() == 1) {
					sqmyRyService.removeById(sqmyRyEntity.getId());
				} else {
					sqmyRyEntity.setSqmyId(sqmyInfo.getId());
					sqmyRyService.saveOrUpdate(sqmyRyEntity);
				}
			}
		}
		return R.status(true);
	}

	/**
	 * 社情民意 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sqmyInfo")
	public R submit(@Valid @RequestBody SqmyInfoEntity sqmyInfo) {
		return R.status(sqmyInfoService.saveOrUpdate(sqmyInfo));
	}

	/**
	 * 社情民意 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@RequestParam String id) {
		return R.status(sqmyInfoService.deleteLogic(Func.toLongList(id)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-sqmyInfo")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导出数据", notes = "传入sqmyInfo")
	public void exportSqmyInfo(@RequestParam Map<String, Object> sqmyInfo, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<SqmyInfoEntity> queryWrapper = Condition.getQueryWrapper(sqmyInfo, SqmyInfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(SqmyInfoEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(SqmyInfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<SqmyInfoExcel> list = sqmyInfoService.exportSqmyInfo(queryWrapper);
		ExcelUtil.export(response, "社情民意数据" + DateUtil.time(), "社情民意数据表", list, SqmyInfoExcel.class);
	}

}
