/*
 *    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.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.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage;
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.entity.TDispatchInfo;
import com.yifu.cloud.plus.v1.yifu.social.service.TDispatchInfoService;
import com.yifu.cloud.plus.v1.yifu.social.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 派单信息记录表
 *
 * @author fxj
 * @date 2022-07-15 11:38:05
 */
@Log4j2
@RestController
@RequiredArgsConstructor
@RequestMapping("/tdispatchinfo" )
@Tag(name = "派单信息记录表管理")
public class TDispatchInfoController {

    private final TDispatchInfoService tDispatchInfoService;

    
    /**
     * 简单分页查询
     * @param page 分页对象
     * @param tDispatchInfo 派单信息记录表
     * @return
     */
    @Operation(description = "简单分页查询")
    @GetMapping("/page")
    public R<IPage<TDispatchInfoPageVo>> getTDispatchInfoPage(Page<TDispatchInfo> page, TDispatchInfoSearchVo tDispatchInfo) {
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
        return  new R<>(tDispatchInfoService.getTDispatchInfoPage(page,tDispatchInfo));
    }

    /**
     * 不分页查询
     * @param tDispatchInfo 派单信息记录表
     * @return
     */
    @Operation(summary = "不分页查询", description = "不分页查询")
    @PostMapping("/noPage" )
    //@PreAuthorize("@pms.hasPermission('demo_tdispatchinfo_get')" )
    public R<List<TDispatchInfo>> getTDispatchInfoNoPage(@RequestBody TDispatchInfoSearchVo tDispatchInfo) {
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
        return R.ok(tDispatchInfoService.noPageDiy(tDispatchInfo));
    }
	/**
	 * 简单分页查询--申请
	 * @param page 分页对象
	 * @param tDispatchInfo 派单信息记录表--申请
	 * @return
	 */
	@Operation(description = "简单分页查询--申请")
	@GetMapping("/pageApply")
	public R<IPage<TDispatchInfoPageVo>> getTDispatchApplyPage(Page<TDispatchInfo> page, TDispatchInfoSearchVo tDispatchInfo) {
		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(user)){
			return  R.failed(CommonConstants.USER_FAIL);
		}
		tDispatchInfo.setCreateBy(user.getId());
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
		return  new R<>(tDispatchInfoService.getTDispatchInfoPage(page,tDispatchInfo));
	}

	/**
	 * 简单分页查询--审核
	 * @param page 分页对象
	 * @param tDispatchInfo 派单信息记录表--审核
	 * @return
	 */
	@Operation(description = "简单分页查询--审核")
	@GetMapping("/pageAudit")
	public R<IPage<TDispatchInfoPageVo>> getTDispatchAuditPage(Page<TDispatchInfo> page, TDispatchInfoSearchVo tDispatchInfo) {
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
		tDispatchInfo.setStatus(CommonConstants.ONE_STRING);
		return  new R<>(tDispatchInfoService.getTDispatchInfoPage(page,tDispatchInfo));
	}

	/**
	 * 简单分页查询--社保办理列表
	 * @param page 分页对象
	 * @param tDispatchInfo 派单信息记录表--社保办理列表
	 * @return
	 */
	@Operation(description = "简单分页查询--社保办理列表")
	@GetMapping("/pageSocialHandle")
	public R<IPage<TDispatchInfo>> getTDispatchSocialHandlePage(Page<TDispatchInfo> page, SocialHandleSearchVo tDispatchInfo) {
		// 获取项目权限 TODO
		List<String> settleDomains = null;
		// 获取自定义sql TODO
		String sql = CommonConstants.EMPTY_STRING;
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
		return  new R<>(tDispatchInfoService.getTDispatchSocialHandlePage(page,tDispatchInfo,settleDomains,sql));
	}

	/**
	 * 简单分页查询--公积金办理列表
	 * @param page 分页对象
	 * @param tDispatchInfo 派单信息记录表--公积金办理列表
	 * @return
	 */
	@Operation(description = "简单分页查询--公积金办理列表")
	@GetMapping("/pageFundHandle")
	public R<IPage<TDispatchInfo>> getTDispatchFundHandlePage(Page<TDispatchInfo> page, SocialHandleSearchVo tDispatchInfo) {
		// 获取项目权限 TODO
		List<String> settleDomains = null;
		// 获取自定义sql TODO
		String sql = CommonConstants.EMPTY_STRING;
		tDispatchInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
		return  new R<>(tDispatchInfoService.getTDispatchFundHandlePage(page,tDispatchInfo,settleDomains,sql));
	}
    /**
     * 通过id查询派单信息记录表
     * @param id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询：hasPermission('demo_tdispatchinfo_get')")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('demo_tdispatchinfo_get')" )
    public R<TDispatchInfo> getById(@PathVariable("id" ) String id) {
        return R.ok(tDispatchInfoService.getById(id));
    }

	/**
	 * 通过id查询派单相关系信息
	 * @author fxj
	 * @param id id
	 * @return R
	 * @date 2022-07-25 10:19
	 */
	@Operation(summary = "通过id查询派单相关系信息", description = "通过id查询派单相关系信息")
	@GetMapping("/getSocialAndFundInfoById" )
	public R<DispatchDetailVo> getSocialAndFundInfoById(@RequestParam(value = "id") String id) {
		return R.ok(tDispatchInfoService.getSocialAndFundInfoById(id));
	}


	/**
     * 新增派单信息记录表
     * @param tDispatchInfo 派单信息记录表
     * @return R
     */
    @Operation(summary = "新增派单信息记录表", description = "新增派单信息记录表：hasPermission('demo_tdispatchinfo_add')")
    @SysLog("新增派单信息记录表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('demo_tdispatchinfo_add')" )
    public R<Boolean> save(@RequestBody TDispatchInfo tDispatchInfo) {
        return R.ok(tDispatchInfoService.save(tDispatchInfo));
    }

    /**
     * 修改派单信息记录表
     * @param tDispatchInfo 派单信息记录表
     * @return R
     */
    @Operation(summary = "修改派单信息记录表", description = "修改派单信息记录表：hasPermission('demo_tdispatchinfo_edit')")
    @SysLog("修改派单信息记录表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('demo_tdispatchinfo_edit')" )
    public R<Boolean> updateById(@RequestBody TDispatchInfo tDispatchInfo) {
        return R.ok(tDispatchInfoService.updateById(tDispatchInfo));
    }

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

    /**
	 * 派单信息记录表 批量导入
	 *
     * @author fxj
     * @date 2022-07-15 11:38:05
	 **/
    @SneakyThrows
    @Operation(description = "批量新增派单信息记录表 hasPermission('demo_tdispatchinfo-batch-import')")
    @SysLog("批量新增派单信息记录表")
    @PostMapping("/importListAdd")
    //@PreAuthorize("@pms.hasPermission('demo_tdispatchinfo-batch-import')")
    public R<List<ErrorMessage>> importListAdd(@RequestBody MultipartFile file, @RequestParam(required = false)String orderId){
        return tDispatchInfoService.importDiy(file.getInputStream(),orderId);
    }

    /**
     * 派单信息记录表 批量导出
     * @author hgw
     * @date 2022-7-28 11:01:56
     **/
    @Operation(description = "导出派单信息记录表 hasPermission('tdispatchinfo-export')")
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('tdispatchinfo-export')")
    public void export(HttpServletResponse response, @RequestBody TDispatchInfoSearchVo searchVo) {
            tDispatchInfoService.listExport(response,searchVo);
    }

	/**
	 * 派单信息记录表 批量导入-派减
	 *
	 * @author fxj
	 * @date 2022-07-15 11:38:05
	 **/
	@SneakyThrows
	@Operation(description = "派单信息记录表 批量导入-派减 hasPermission('demo_tdispatchinfo-batch-reduce')")
	@SysLog("派单信息记录表 批量导入-派减")
	@PostMapping("/importListReduce")
	//@PreAuthorize("@pms.hasPermission('demo_tdispatchinfo-batch-reduce')")
	public R<List<ErrorMessage>> importListReduce(@RequestBody MultipartFile file){
		return tDispatchInfoService.importReduceDiy(file.getInputStream());
	}

	/**
	 * @Author fxj
	 * @Description 派单申请批量审核
	 * @Date 16:54 2022/7/25
	 * @Param
	 * @return
	**/
	@Operation(description = "派单申请批量审核，传参派单ID hasPermission('wxhr:tdispatchinfo_addApplyAudit') auditStatus:1审核通过 2审核不通过")
	@PostMapping("/addApplyAudit")
	//@PreAuthorize("@pms.hasPermission('wxhr:tdispatchinfo_addApplyAudit')")
	public R<List<ErrorMessage>> addApplyAudit(@RequestParam(name = "ids", required = true) String ids,
									@RequestParam(name = "auditStatus", required = true) String auditStatus,
									@RequestParam(name = "auditRemark", required = false) String auditRemark,
									@RequestParam(name = "remark", required = false) String remark) {
		return  tDispatchInfoService.addApplyAudit(ids,auditStatus,auditRemark,remark);
	}

	/**
	 * 派单申请办理
	 * hgw社保办理
	 * @param ids
	 * @param handleStatus 1 办理成功 2 办理失败
	 * @param handleRemark
	 * @param typeSub 0 社保 1  公积金
	 * @param socialType （1养老 2医疗 3失业 4工伤 5生育 6大病）
	 * @return
	 * @Author fxj
	 * @Date 2019-09-27
	 **/
	@Operation(description = "派单批量办理，传参派单ID hasPermission('wxhr:tdispatchinfo_addApplyHandle') handleStatus 1 办理成功 2 办理失败")
	@PostMapping("/addApplyHandle")
	//@PreAuthorize("@pms.hasPermission('wxhr:tdispatchinfo_addApplyHandle')")
	public R<List<ErrorMessage>> addApplyHandle(@RequestParam(name = "ids", required = true) String ids,
									 @RequestParam(name = "typeSub", required = true) String typeSub,
									 @RequestParam(name = "handleStatus", required = true) String handleStatus,
									 @RequestParam(name = "handleRemark", required = false) String handleRemark,
									 @RequestParam(name = "socialType", required = false) String socialType,
									 @RequestParam(name = "remark", required = false) String remark) {
		return tDispatchInfoService.addApplyHandle(ids,typeSub,handleStatus,handleRemark,socialType,remark);
	}

	/**
	 * @Author fxj
	 * @Description  导出 社保花名册
	 * @Date 17:40 2022/7/27
	 * @Param
	 * @return
	**/
	@PostMapping("/doexportSocialRecordRoster")
	@Operation(description = "导出社保花名册 hasPermission('wxhr:tdispatchinfo_doexportSocialRecordRoster')")
	@SysLog("导出社保花名册信息")
	//@PreAuthorize("@pms.hasPermission('wxhr:tdispatchinfo_doexportSocialRecordRoster')")
	public void doexportSocialRecordRoster(HttpServletResponse response, HttpServletRequest request,
										   @RequestBody  SocialHandleSearchVo searchVo) {
		tDispatchInfoService.doexportSocialRecordRoster(response,searchVo,searchVo.getIdStr(),null);
	}

	/**
	 * @Author fxj
	 * @Description 导出 公积金变更清册
	 * @Date 20:05 2022/7/27
	 * @Param
	 * @return
	**/
	@PostMapping("/doexportFundRecordChange")
	@Operation(description = "导出公积金变更清册 hasPermission('wxhr:tdispatchinfo_doexportFundRecordChange')")
	@SysLog("导出公积金变更清册")
	@PreAuthorize("@pms.hasPermission('wxhr:tdispatchinfo_doexportFundRecordChange')")
	public void doexportFundRecordChange(HttpServletResponse response, HttpServletRequest request,
										 @RequestBody SocialHandleSearchVo searchVo) {
		// 获取项目权限 TODO
		List<String> settleDomains = null;
		// 获取自定义sql TODO
		String sql = CommonConstants.EMPTY_STRING;
		tDispatchInfoService.doexportFundRecordChange(response, searchVo.getIdStr(),searchVo,settleDomains,sql);
	}
	/**
	 * @Author fxj
	 * @Description 导出公积金补缴清册
	 * @Date 20:05 2022/7/27
	 * @Param
	 * @return
	**/
	@GetMapping("/doExportFundSupplementaryRecord")
	@Operation(description = "导出公积金补缴清册 hasPermission('wxhr:tdispatchinfo_doExportFundSupplementaryRecord')")
	@SysLog("导出公积金补缴清册")
	@PreAuthorize("@pms.hasPermission('wxhr:tdispatchinfo_doExportFundSupplementaryRecord')")
	public R<List<FundSupplementaryExportVo>> doExportFundSupplementaryRecord(HttpServletResponse response, HttpServletRequest request,
																			  @RequestParam(name = "idStr", required = false) String idStr,
																			  @RequestBody SocialHandleSearchVo searchVo) {
		// 获取项目权限 TODO
		List<String> settleDomains = null;
		// 获取自定义sql TODO
		String sql = CommonConstants.EMPTY_STRING;
		return tDispatchInfoService.getFundSupplementaryRecords(idStr,searchVo,settleDomains,sql);
	}
}
