/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.yifu.cloud.plus.v1.yifu.social.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ExcelUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.log.annotation.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.social.constants.PreDispatchConstants;
import com.yifu.cloud.plus.v1.yifu.social.entity.TPreDispatchInfo;
import com.yifu.cloud.plus.v1.yifu.social.service.TPreDispatchInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * 预派单记录
 *
 * @author huyc
 * @date 2022-07-14 18:53:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/tpredispatchinfo" )
@Tag(name = "预派单记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TPreDispatchInfoController {

    private final  TPreDispatchInfoService tPreDispatchInfoService;

    /**
     * 简单分页查询
     * @param page 分页对象
     * @param tPreDispatchInfo 预派单记录
     * @return
     */
	@Operation(summary = "简单分页查询", description = "简单分页查询")
    @GetMapping("/page")
    public R<IPage<TPreDispatchInfo>> getTPreDispatchInfoPage(Page<TPreDispatchInfo> page, TPreDispatchInfo tPreDispatchInfo) {
        return  new R<>(tPreDispatchInfoService.getTPreDispatchInfoPage(page,tPreDispatchInfo));
    }
    /**
     * 不分页查询
     * @param tPreDispatchInfo 预派单记录
     * @return
     */
    @Operation(summary = "不分页查询", description = "不分页查询")
    @PostMapping("/noPage" )
    public R<List<TPreDispatchInfo>> getTPreDispatchInfoNoPage(@RequestBody TPreDispatchInfo tPreDispatchInfo) {
        return R.ok(tPreDispatchInfoService.list(Wrappers.query(tPreDispatchInfo)));
    }

    /**
     * 通过id查询预派单记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询：hasPermission('social_tpredispatchinfo_get')")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_get')" )
    public R<TPreDispatchInfo> getById(@PathVariable("id" ) String id) {
        return R.ok(tPreDispatchInfoService.getById(id));
    }

    /**
     * 新增预派单记录
     * @param tPreDispatchInfo 预派单记录
     * @return R
     */
    @Operation(summary = "新增预派单记录", description = "新增预派单记录：hasPermission('social_tpredispatchinfo_add')")
    @SysLog("新增预派单记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_add')" )
    public R<Boolean> save(@RequestBody TPreDispatchInfo tPreDispatchInfo) {
        return R.ok(tPreDispatchInfoService.save(tPreDispatchInfo));
    }

    /**
     * 修改预派单记录
     * @param tPreDispatchInfo 预派单记录
     * @return R
     */
    @Operation(summary = "修改预派单记录", description = "修改预派单记录：hasPermission('social_tpredispatchinfo_edit')")
    @SysLog("修改预派单记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_edit')" )
    public R<Boolean> updateById(@RequestBody TPreDispatchInfo tPreDispatchInfo) {
        return tPreDispatchInfoService.update(tPreDispatchInfo);
    }

    /**
     * 通过id删除预派单记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id删除预派单记录", description = "通过id删除预派单记录：hasPermission('social_tpredispatchinfo_del')")
    @SysLog("通过id删除预派单记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_del')" )
    public R<Boolean> removeById(@PathVariable String id) {
        return R.ok(tPreDispatchInfoService.removeById(id));
    }

	/**
	 * 修改资料是否提交：0 是 1  否
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param ids
	 * @param status 0 是 1 否
	 * @return R
	 */
	@Operation(summary = "修改资料是否提交", description = "修改资料是否提交：status 0 是 1 否(social_tpredispatchinfo_edit)")
	@SysLog("修改资料是否提交")
	@PostMapping("/modifyDataSubmitStatus")
	@PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_edit')")
	public R<Boolean> modifyDataSubmitStatus(@RequestParam(value = "ids",required = true)String ids,
											 @RequestParam(value = "status",required = true)String status) {
		return tPreDispatchInfoService.modifyDataSubmitStatus(ids,status);
	}
	/**
	 * 清空预派单默认的派单合同信息
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param ids
	 * @return R
	 **/
	@Operation(summary = "清空预派单默认的派单合同信息", description = "清空预派单默认的派单合同信息(social_tpredispatchinfo_edit)")
	@SysLog("清空预派单默认的派单合同信息")
	@PostMapping("/clearContractInfo")
	@PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_edit')")
	public R<Boolean> clearContractInfo(@RequestParam(value = "ids",required = true)String ids) {
		return tPreDispatchInfoService.clearContractInfo(ids);
	}

	/**
	 * 更新预派单是否派单状态
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param id
	 * @param dispatchFlag 0 派单  1  不派单
	 * @param cancelRemark 不派单 时填写
	 * @return R
	 **/
	@Operation(summary = "更新预派单是否派单状态", description = "更新预派单是否派单状态(social_tpredispatchinfo_edit)")
	@SysLog("更新预派单是否派单状态")
	@PostMapping("/updateDispatchFlag")
	@PreAuthorize("@pms.hasPermission('social_tpredispatchinfo_edit')")
	public R<Boolean> updateDispatchFlag(@RequestParam(value = "id",required = true)String id,
										 @RequestParam(value = "dispatchFlag",required = true)String dispatchFlag,
										 @RequestParam(value = "cancelRemark",required = false)String cancelRemark) {
		return tPreDispatchInfoService.updateDispatchFlag(id,dispatchFlag,cancelRemark);
	}

	/**
	 * 单个派单
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param id
	 * @return R
	 */
	@Operation(summary = "单个派单:按创建人和ID", description = "单个派单:按创建人和ID")
	@PostMapping("/dispatchById")
	public R<Boolean> dispatchById(@RequestParam(value = "id",required = true)String id) {
		return tPreDispatchInfoService.dispatchById(id);
	}

	/**
	 * 预派单批量新增派增
	 *
	 * @param jsonString
	 * @param houseId 社保户ID
	 * @param departId 结算主体ID
	 * @return
	 * @Author fxj
	 * @Date 2019-09-29
	 **/
	@Operation(summary = "预派单批量新增派增", description = "预派单批量新增派增")
	@SysLog("预派单批量新增派增")
	@PostMapping("/analysisJsonStringImportAdd")
	@PreAuthorize("@pms.hasPermission('social:tpredispatchinfo_importAddList')")
	public R analysisJsonStringImportAdd(@RequestBody String jsonString,
												  @RequestParam(value = "houseId",required = true) String houseId,
												  @RequestParam(value = "departId",required = true) String departId) {
		YifuUser user = SecurityUtils.getUser();
		if (null == user || null == user.getId()) {
			return R.failed(CommonConstants.USER_FAIL);
		}
		ExcelUtil<TPreDispatchInfo> util1 = null;
		try {
			jsonString = URLDecoder.decode(jsonString, CommonConstants.UTF8).replace("=", "");
			util1 = new ExcelUtil<>(TPreDispatchInfo.class);
			util1.getJsonStringToList(jsonString, null);
			List<TPreDispatchInfo> listInfo = util1.getEntityList();
			//用于返回错误信息
			List<ErrorMessage> errorInfo = new ArrayList<>();
			if (null != util1.getErrorInfo() && !util1.getErrorInfo().isEmpty()) {
				return R.failed(util1.getErrorInfo(),PreDispatchConstants.DATA_CHECK);
			} else {
				if (null != listInfo && !listInfo.isEmpty()) {
					errorInfo = tPreDispatchInfoService.batchSavePreDisPatchAdd(listInfo, user,houseId,departId);
				} else {
					return R.failed(CommonConstants.DATA_CAN_NOT_EMPTY);
				}
				return R.ok(errorInfo,"");
			}
		} catch (Exception e) {
			return R.failed(PreDispatchConstants.DATA_IMPORT_ANALYSIS_ERROR);
		}
	}
}
