package org.springblade.modules.log.sjSp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
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.csInfo.entity.CsInfoEntity;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.csjcInfo.entity.CsjcInfoEntity;
import org.springblade.modules.csjcInfo.service.ICsjcInfoService;
import org.springblade.modules.log.sjSp.entity.SjSpEntity;
import org.springblade.modules.log.sjSp.excel.SjSpExcel;
import org.springblade.modules.log.sjSp.service.ISjSpService;
import org.springblade.modules.log.sjSp.vo.SjSpVO;
import org.springblade.modules.log.sjSp.wrapper.SjSpWrapper;
import org.springblade.modules.mdjfdj.service.IMdjfDjService;
import org.springblade.modules.mdjfdj.vo.MdXgVO;
import org.springblade.modules.mdjfry.entity.MdjfRyEntity;
import org.springblade.modules.mdjfry.service.IMdjfRyService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 数据审批 控制器
 *
 * @author BladeX
 * @since 2024-04-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-sjSp/sjSp")
@Api(value = "数据审批", tags = "数据审批接口")
public class SjSpController extends BladeController {

	private final ISjSpService sjSpService;
	private final ICsInfoService csInfoService;
	private final ICsjcInfoService csjcInfoService;
	private final IMdjfDjService mdjfdjService;
	private final IMdjfRyService mdjfryService;
	private final IDeptService deptService;
	private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * 数据审批 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sjSp")
	public R<SjSpVO> detail(SjSpEntity sjSp) {
		SjSpEntity detail = sjSpService.getOne(Condition.getQueryWrapper(sjSp));
		return R.data(SjSpWrapper.build().entityVO(detail));
	}

	/**
	 * 数据审批 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sjSp")
	public R<IPage<SjSpVO>> list(@ApiIgnore @RequestParam Map<String, Object> sjSp, Query query) {
		IPage<SjSpEntity> pages = sjSpService.page(Condition.getPage(query), Condition.getQueryWrapper(sjSp, SjSpEntity.class));
		return R.data(SjSpWrapper.build().pageVO(pages));
	}

	/**
	 * 数据审批 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sjSp")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		Dept dept = deptService.getDeptCategory(AuthUtil.getDeptId());
		if (null != dept && dept.getDeptCategory() == 5) {
			params.put("deptId", AuthUtil.getDeptId());
			IPage<SjSpVO> pages = sjSpService.selectSjSpPage(Condition.getPage(query), params);
			return R.data(pages);
		} else {
			return R.fail("当前账号所属部门不正确");
		}
	}

	/**
	 * 数据审批--平台
	 */
	@GetMapping("/pageToPt")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sjSp")
	public R<IPage<SjSpVO>> pageToPt(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<SjSpVO> pages = sjSpService.selectSjSpPageToPt(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 数据审批 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sjSp")
	public R save(@Valid @RequestBody SjSpEntity sjSp) {
		return R.status(sjSpService.save(sjSp));
	}

	/**
	 * 数据审批 修改
	 */
	@DSTransactional
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sjSp")
	public R update(@Valid @RequestBody SjSpEntity sjSp) throws ParseException {
		Dept dept = deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
		sjSpService.updateById(sjSp);
		if (sjSp.getSpjg() == 1) {
			if (sjSp.getYwlb() == 1) {
				// 九小删除
				CsInfoEntity csInfoEntity = new CsInfoEntity();
				csInfoEntity.setId(sjSp.getYwid());
				csInfoEntity.setIsDeleted(1);
				csInfoEntity.setUpdateUser(sjSp.getCreateUser());
				csInfoService.deleteBySp(csInfoEntity);
				if (dept.getAncestors().contains("220200000000")) {
					// 向吉林数据库删除
					csInfoService.deleteBySpToJl(csInfoEntity);
				}
			} else if (sjSp.getYwlb() == 2) {
				// 九小营业状态变更
				CsInfoEntity csInfoEntity = new CsInfoEntity();
				csInfoEntity.setId(sjSp.getYwid());
				csInfoEntity.setYyzt(Integer.valueOf(sjSp.getParams()));
				csInfoEntity.setUpdateUser(sjSp.getCreateUser());
				if (csInfoEntity.getYyzt() == 1) {
					CsjcInfoEntity csjcInfoEntity = new CsjcInfoEntity();
					csjcInfoEntity.setCsId(csInfoEntity.getId());
					csjcInfoEntity.setBz("商家歇业");
					csjcInfoEntity.setZgzt(0);
					csjcInfoService.save(csjcInfoEntity);
					csInfoEntity.setJcId(csjcInfoEntity.getId());
				} else if (csInfoEntity.getYyzt() == 2) {
//					// 停业
//					List<String> jidu = csjcInfoService.getJidu();
//					for (String day : jidu) {
//						CsjcInfoEntity csjcInfoEntity = new CsjcInfoEntity();
//						csjcInfoEntity.setCsId(csInfoEntity.getId());
//						csjcInfoEntity.setBz("商家停业");
//						csjcInfoEntity.setZgzt(0);
//						csjcInfoEntity.setCreateTime(simpleDateFormat.parse(day));
//						csjcInfoService.save(csjcInfoEntity);
//						csjcInfoService.updateCreateTime(csjcInfoEntity.getId(), simpleDateFormat.parse(day));
//						csInfoEntity.setJcId(csjcInfoEntity.getId());
//					}
				}
				csInfoService.updateById(csInfoEntity);
				if (dept.getAncestors().contains("220200000000")) {
					// 向吉林数据库修改 营业状态
					csInfoService.updateByIdToJl(csInfoEntity);
				}
			} else if (sjSp.getYwlb() == 3) {
				// 矛盾纠纷修改
				String params = sjSp.getParams();
//				String params = "{\"mdjfDj\":{\"bjrdh\":\"\",\"bz\":\"备注\",\"id\":1748260725229588481,\"jffsdd\":\"吉林省公安厅出入境管理局\",\"jffssj\":1713941580000,\"jflx\":\"房屋拆迁\",\"jfly\":\"自主排查\",\"jfmc\":\"纠纷\",\"jfsssq\":220221690001,\"jyaq\":\"案情\",\"mdjbV\":\"D\",\"sfjj\":0,\"sfmy\":0,\"sfsc\":0,\"zzdw\":\"单位\"},\"mdjfRyList\":[{\"lb\":\"甲方\",\"lxdh\":\"18612345678\",\"sflk\":0,\"sfzh\":\"220284198901017076\",\"xm\":\"甲方\"},{\"lb\":\"乙方\",\"lxdh\":\"18612345678\",\"sflk\":0,\"sfzh\":\"220284198901017076\",\"xm\":\"乙方\"}],\"qqyy\":\"请求修改矛盾\"}";
				JSONObject jsonObject = JSON.parseObject(params);
				MdXgVO mdXgVO = JSON.toJavaObject(jsonObject, MdXgVO.class);
				// 修改矛盾基本信息
				mdXgVO.getMdjfDj().setUpdateUser(sjSp.getCreateUser());
				mdjfdjService.updateById(mdXgVO.getMdjfDj());
				if (dept.getAncestors().contains("220200000000")) {
					mdjfdjService.updateByIdToJl(mdXgVO.getMdjfDj());
				}
				if (null != mdXgVO.getMdjfRyList() && !mdXgVO.getMdjfRyList().isEmpty()) {
					// 修改人员
					for (MdjfRyEntity mdjfRy : mdXgVO.getMdjfRyList()) {
						mdjfRy.setUpdateUser(sjSp.getCreateUser());
						mdjfryService.updateById(mdjfRy);
						if (dept.getAncestors().contains("220200000000")) {
							mdjfryService.updateByIdToJl(mdjfRy);
						}
					}
				}
			}
		}
		return R.status(true);
	}

	/**
	 * 临时接口
	 */
	/*@Transactional
	@PostMapping("/tysj")
	public R tysj(MultipartFile file) throws ParseException {
		List<TysjExcel> list = ExcelUtil.read(file, TysjExcel.class);
		for (int i = 0; i < list.size() ; i++) {
			System.out.println(i + 1);
			csInfoService.hfDeleted(list.get(i).getId());
			// 停业
			List<String> jidu = csjcInfoService.getJidu();
			Long jcId = 0L;
			for (String day : jidu) {
				CsjcInfoEntity csjcInfoEntity = new CsjcInfoEntity();
				csjcInfoEntity.setCsId(list.get(i).getId());
				csjcInfoEntity.setBz("商家停业");
				csjcInfoEntity.setZgzt(0);
				csjcInfoService.save(csjcInfoEntity);
				csjcInfoService.updateCreateTime(csjcInfoEntity.getId(), simpleDateFormat.parse(day));
				jcId = csjcInfoEntity.getId();
			}
			csInfoService.updateLastJcId(list.get(i).getId(), jcId);
		}
		return R.success("操作成功");
	}*/

	/**
	 * 数据审批 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sjSp")
	public R submit(@Valid @RequestBody SjSpEntity sjSp) {
		return R.status(sjSpService.saveOrUpdate(sjSp));
	}

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

	/**
	 * 导出数据
	 */
	@GetMapping("/export-sjSp")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入sjSp")
	public void exportSjSp(@ApiIgnore @RequestParam Map<String, Object> sjSp, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<SjSpEntity> queryWrapper = Condition.getQueryWrapper(sjSp, SjSpEntity.class);
		queryWrapper.lambda().eq(SjSpEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<SjSpExcel> list = sjSpService.exportSjSp(queryWrapper);
		ExcelUtil.export(response, "数据审批数据" + DateUtil.time(), "数据审批数据表", list, SjSpExcel.class);
	}

}
