package com.yeejoin.amos.boot.module.ugp.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.boot.module.ugp.api.Enum.NoticeStatusEnum;
import com.yeejoin.amos.boot.module.ugp.api.dto.EquipmentDto;
import com.yeejoin.amos.boot.module.ugp.api.dto.InstallNoticePageDto;
import com.yeejoin.amos.boot.module.ugp.api.dto.ProjectDto;
import com.yeejoin.amos.boot.module.ugp.api.entity.InstallNotice;
import com.yeejoin.amos.boot.module.ugp.api.entity.Project;
import com.yeejoin.amos.boot.module.ugp.api.mapper.InstallNoticeMapper;
import com.yeejoin.amos.boot.module.ugp.api.mapper.ProjectMapper;
import com.yeejoin.amos.boot.module.ugp.biz.service.impl.ProjectInitiationServiceImpl;
import com.yeejoin.amos.boot.module.ugp.biz.service.impl.ProjectServiceImpl;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import com.yeejoin.amos.boot.biz.common.controller.BaseController;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yeejoin.amos.boot.module.ugp.biz.service.impl.InstallNoticeServiceImpl;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeejoin.amos.boot.module.ugp.api.dto.InstallNoticeDto;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.foundation.enumeration.UserType;

/**
 * 安装告知表
 *
 * @author system_generator
 * @date 2022-09-22
 */
@RestController
@Api(tags = "安装告知表Api")
@RequestMapping(value = "/install-notice")
public class InstallNoticeController extends BaseController {

	@Autowired
	InstallNoticeServiceImpl installNoticeServiceImpl;

	@Autowired
	ProjectMapper projectMapper;

	@Autowired
	ProjectInitiationServiceImpl projectInitiationServiceImpl;

	@Autowired
	ProjectServiceImpl projectServiceImpl;


	/**
	 * 新增安装告知表
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@PostMapping(value = "/save")
	@ApiOperation(httpMethod = "POST", value = "新增安装告知表", notes = "新增安装告知表")
	public ResponseModel<InstallNoticeDto> save(@RequestBody InstallNoticeDto model) {
		model = installNoticeServiceImpl.createWithModel(model);
		return ResponseHelper.buildResponse(model);
	}

	/**
	 * 根据sequenceNbr更新
	 *
	 * @param sequenceNbr 主键
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@PutMapping(value = "/{sequenceNbr}")
	@ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新", notes = "根据sequenceNbr更新")
	public ResponseModel<InstallNoticeDto> updateBySequenceNbrInstallNotice(@RequestBody InstallNoticeDto model, @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
		return ResponseHelper.buildResponse(installNoticeServiceImpl.updateWithModel(model));
	}

	/**
	 * 根据sequenceNbr删除
	 *
	 * @param sequenceNbr 主键
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@DeleteMapping(value = "/{sequenceNbr}")
	@ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除安装告知表", notes = "根据sequenceNbr删除安装告知表")
	public ResponseModel<Boolean> deleteBySequenceNbr(HttpServletRequest request, @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
		return ResponseHelper.buildResponse(installNoticeServiceImpl.removeById(sequenceNbr));
	}

	/**
	 * 根据sequenceNbr查询
	 *
	 * @param sequenceNbr 主键
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@GetMapping(value = "/{sequenceNbr}")
	@ApiOperation(httpMethod = "GET", value = "根据sequenceNbr查询单个安装告知表", notes = "根据sequenceNbr查询单个安装告知表")
	public ResponseModel<InstallNoticeDto> selectOne(@PathVariable Long sequenceNbr) {
		return ResponseHelper.buildResponse(installNoticeServiceImpl.queryBySeq(sequenceNbr));
	}
	/**
	 * 列表分页查询
	 *
	 * @param current 当前页
	 * @param current 每页大小
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@GetMapping(value = "/page")
	@ApiOperation(httpMethod = "GET", value = "安装告知表分页查询", notes = "安装告知表分页查询")
	public ResponseModel<Page<InstallNoticeDto>> queryForPage(@RequestParam(value = "current") int current, @RequestParam
			(value = "size") int size) {
		Page<InstallNoticeDto> page = new Page<InstallNoticeDto>();
		page.setCurrent(current);
		page.setSize(size);
		return ResponseHelper.buildResponse(installNoticeServiceImpl.queryForInstallNoticePage(page));
	}

	/**
	 * 列表分页查询
	 *
	 * @param current 当前页
	 * @param current 每页大小
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@GetMapping(value = "/installNoticePage")
	@ApiOperation(httpMethod = "GET", value = "安装告知表分页查询", notes = "安装告知表分页查询")
	public ResponseModel<Page<InstallNoticePageDto>> queryForPage1(@RequestParam(value = "current") int current, @RequestParam
			(value = "size") int size,@RequestParam(required = false) String name,@RequestParam(required = false) String constructionUnit) {
		Page<InstallNoticePageDto> page = new Page<InstallNoticePageDto>();
		page.setCurrent(current);
		page.setSize(size);
		return ResponseHelper.buildResponse(installNoticeServiceImpl.installNoticePage(page,name,constructionUnit));
	}
	/**
	 * 列表全部数据查询
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@ApiOperation(httpMethod = "GET", value = "安装告知表列表全部数据查询", notes = "安装告知表列表全部数据查询")
	@GetMapping(value = "/list")
	public ResponseModel<List<InstallNoticeDto>> selectForList() {
		return ResponseHelper.buildResponse(installNoticeServiceImpl.queryForInstallNoticeList());
	}

	/**
	 * 查询安装告知申请列表
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@GetMapping(value = "/installApplyList")
	@ApiOperation(httpMethod = "GET", value = "安装告知表分页查询", notes = "安装告知表分页查询")
	public ResponseModel<List<InstallNoticePageDto>> queryForPage1() {
		return ResponseHelper.buildResponse(installNoticeServiceImpl.installNoticeList());
	}

	/**
	 * 根据主键sequenceNbr查询安装告知申请详情
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@GetMapping ("")
	@ApiOperation(httpMethod = "GET", value = "查询安装告知申请详情", notes = "查询安装告知申请详情")
	public ResponseModel<InstallNoticeDto> installNoticeinfoById(@RequestParam ("sequenceNbr") Long sequenceNbr){
		return ResponseHelper.buildResponse(installNoticeServiceImpl.selectOneById(sequenceNbr));
	}

	/**
	 * 提交申请
	 *
	 * @param sequenceNbr 主键
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@PutMapping(value = "/submit/{sequenceNbr}")
	@ApiOperation(httpMethod = "put", value = "根据sequenceNbr更新提交申请", notes = "根据sequenceNbr更新提交申请")
	public ResponseModel<InstallNoticeDto> InstallNoticeSubmit(@RequestBody InstallNoticeDto model, @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
		if (model.getNoticeStatus().isEmpty()){
			model.setNoticeStatus("已提交");
			ResponseHelper.buildResponse(installNoticeServiceImpl.updateWithModel(model));
		}
		return ResponseHelper.buildResponse(model);
	}

	/**
	 * 新增安装告知申请,保存/提交
	 *
	 * @return
	 */
	@TycloudOperation(ApiLevel = UserType.AGENCY,needAuth = false)
	@PostMapping (value = "/saveInstallNotice")
	@ApiOperation(httpMethod = "Post", value = "新增安装告知申请", notes = "新增安装告知申请")
	public ResponseModel<InstallNoticeDto> saveInstallNotice(@RequestBody JSONObject object,@RequestParam(required = false) String noticeStatus ) {
		InstallNoticeDto installNoticeDto = new InstallNoticeDto();

		final ProjectDto projectDto = projectMapper.selectOneByName((String) object.get("name"));

		installNoticeDto.setProjectId(projectDto.getSequenceNbr());
		installNoticeDto.setNoticeStatus(noticeStatus);

		final JSONArray subForm = object.getJSONArray("subForm");

		installNoticeDto.setLicenseNum(object.getString("licenseNum"));
		installNoticeDto.setLicenseCompany(object.getString("licenseCompany"));
		for(Object o:subForm){
			JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(o));
			final JSONArray licenseAttch = jsonObject.getJSONArray("licenseAttch");
			final JSONArray contractAttch = jsonObject.getJSONArray("contractAttch");

			installNoticeDto.setLicenseAttch(JSON.toJSONString(licenseAttch));
			installNoticeDto.setContractAttch(JSON.toJSONString(contractAttch));
			break;
			}
		projectInitiationServiceImpl.execute(projectDto.getInstanceId(),installNoticeDto,"1");
		return  ResponseHelper.buildResponse(installNoticeServiceImpl.createWithModel(installNoticeDto));
	}

	@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
	@PostMapping(value = "/acceptNotification")
	@ApiOperation(httpMethod = "get", value = "接受告知", notes = "接受告知")
	public ResponseModel<Boolean> acceptNotification(Long sequenceNbr){
		InstallNotice installNotice = installNoticeServiceImpl.getById(sequenceNbr);
		installNotice.setNoticeStatus(NoticeStatusEnum.已接收.getName());
		Project project = projectServiceImpl.getById(installNotice.getProjectId());
		projectInitiationServiceImpl.execute(project.getInstanceId(),installNotice,NoticeStatusEnum.已接收.getStatusId());
		return ResponseHelper.buildResponse(installNoticeServiceImpl.updateById(installNotice));
	}
}
