/*
 *    Copyright (c) 2018-2025, hnqz 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 pig4cloud.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: hnqz
 */

package com.qunzhixinxi.hnqz.admin.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackageStatus;
import com.qunzhixinxi.hnqz.admin.enums.PackageTypeEnum;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
import com.qunzhixinxi.hnqz.admin.service.WmScorePackageStatusService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 积分包状态
 *
 * @author pigx code generator
 * @date 2020-07-17 23:32:01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmscorepackagestatus" )
@Api(value = "wmscorepackagestatus", tags = "积分包状态管理")
public class WmScorePackageStatusController {

    private final  WmScorePackageStatusService wmScorePackageStatusService;
	private final WmTaskService wmTaskService;
	private final WmScorePackageService wmScorePackageService;

	/**
     * 分页查询
     * @param page 分页对象
     * @param wmScorePackageStatus 积分包状态
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getWmScorePackageStatusPage(Page page, WmScorePackageStatus wmScorePackageStatus) {
		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
		wmScorePackageStatus.setUserId(String.valueOf(SecurityUtils.getUser().getId()));
        return R.ok(wmScorePackageStatusService.getWmScorePackageStatusPage(page,wmScorePackageStatus));
    }


    /**
     * 通过id查询积分包状态
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(wmScorePackageStatusService.getById(id));
    }

    /**
     * 新增积分包状态
     * @param wmScorePackageStatus 积分包状态
     * @return R
     */
    @ApiOperation(value = "新增积分包状态", notes = "新增积分包状态")
    @SysLog("新增积分包状态" )
    @PostMapping
    public R save(@RequestBody WmScorePackageStatus wmScorePackageStatus) {
		wmScorePackageStatus.setCreateTime(LocalDateTime.now());
        return R.ok(wmScorePackageStatusService.save(wmScorePackageStatus));
    }

    /**
     * 修改积分包状态
     * @param wmScorePackageStatus 积分包状态
     * @return R
     */
    @ApiOperation(value = "修改积分包状态", notes = "修改积分包状态")
    @SysLog("修改积分包状态" )
    @PutMapping
    public R updateById(@RequestBody WmScorePackageStatus wmScorePackageStatus) {
			boolean answer=wmScorePackageStatusService.updateById(wmScorePackageStatus);
			if(answer)
			{
				WmScorePackage wmScorePackage=wmScorePackageService.getById(wmScorePackageStatus.getPackageId());
				if("2".equals(wmScorePackageStatus.getStatus())) {
					wmScorePackage.setZbId(wmScorePackageStatus.getUserId());
					wmScorePackage.setPackageStatus("2");
					wmScorePackage.setUpdateTime(LocalDateTime.now());
					wmScorePackage.setReceiveDate(LocalDateTime.now());
					wmScorePackage.setIsConduct("1");
				}else{
					wmScorePackage.setPackageStatus(null);
					wmScorePackage.setIsConduct(null);
				}
				wmScorePackageService.updateById(wmScorePackage);

			}
        return R.ok();
    }

    /**
     * 通过id删除积分包状态
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除积分包状态", notes = "通过id删除积分包状态")
    @SysLog("通过id删除积分包状态" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(wmScorePackageStatusService.removeById(id));
    }


	/**
	 * 接单审核--分页查询
	 * @param page 分页对象
	 * @param wmScorePackageStatus 积分包状态
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getPage" )
	public R getPage(Page page, WmScorePackageStatus wmScorePackageStatus){
		if(StringUtil.isEmpty(wmScorePackageStatus.getPackageId())){
			return R.failed("未查询到积分包id");
		}
		return wmScorePackageStatusService.getPage(page, wmScorePackageStatus);
	}

	/**
	 * 接单审核--同意
	 * @param wmScorePackageStatus
	 * @return
	 */
	@PostMapping("/approvalStatus")
	public R approvalStatus(@RequestBody WmScorePackageStatus wmScorePackageStatus){
		if(wmScorePackageStatus.getId()==null){
			return R.failed("缺少参数ID");
		}
		if(StringUtil.isEmpty(wmScorePackageStatus.getStatus())){
			return R.failed("缺少参数:审批意见");
		}
		return wmScorePackageStatusService.approvalStatus( wmScorePackageStatus);
	}

	/**
	 * 终止接单--区分全部收回，和部分收回
	 *
	 * @param wmScorePackageStatus 终止接单
	 * enableType 1全部收回，2部分收回
	 * @return R
	 */
	@ApiOperation(value = "终止接单", notes = "终止接单")
	@SysLog("终止接单" )
	@GetMapping("updateZZ")
	public R updateZZ( WmScorePackageStatus wmScorePackageStatus) {
		if(StringUtil.isEmpty(wmScorePackageStatus.getId()+"")){
			return R.failed("缺少参数");
		}
		return wmScorePackageStatusService.updateZZ(wmScorePackageStatus);
	}

	/**
	 * 获取当前部门已接单人的列表
	 *
	 * @return 获取当前部门已接单人的列表
	 */
	@ApiOperation(value = "获取当前部门已接单人的列表", notes = "获取当前部门已接单人的列表")
	@GetMapping("/getDeptReceiveUserList")
	public R getDeptReceiveUserList() {
		Integer deptId = SecurityUtils.getUser().getDeptId();
		List<Map<String, String>> list = wmScorePackageStatusService.getDeptReceiveUserList(String.valueOf(deptId),
				PackageTypeEnum.TYPE1_HCP_PACKAGE.val(), PackageTypeEnum.TYPE2_SETTLE_PACKAGE.val());
		return R.ok(list);
	}
}
