package com.hotent.uc.controller;

import java.util.Optional;

import javax.annotation.Resource;

import com.hotent.base.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.uc.manager.ElectronicSealManager;
import com.hotent.uc.model.ElectronicSeal;
import com.hotent.uc.model.User;
import com.hotent.uc.params.seal.ImportSignatureObject;
import com.hotent.uc.util.ContextUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * <pre> 
 * 描述：uc_electronic_seal 控制器类
 * 构建组：x7
 * 作者:lihq
 * 邮箱:lihq@jee-soft.cn
 * 日期:2020-10-19 18:18:31
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@RestController
@RequestMapping(value="/uc/electronicSeal/v1")
@Api(tags="electronicSealController")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class ElectronicSealController extends BaseController<ElectronicSealManager,ElectronicSeal>{
	@Resource
	ElectronicSealManager electronicSealManager;
	@Autowired
	PasswordEncoder passwordEncoder;
	
	/**
	 * uc_electronic_seal列表(分页条件查询)数据
	 * @param
	 * @return
	 * @throws Exception 
	 * PageJson
	 * @exception 
	 */
	@PostMapping("/listJson")
	@ApiOperation(value="uc_electronic_seal数据列表", httpMethod = "POST", notes = "获取uc_electronic_seal列表")
	public PageList<ElectronicSeal> list(@ApiParam(name="queryFilter",value="查询对象")@RequestBody QueryFilter queryFilter) throws Exception{
		return electronicSealManager.query(queryFilter);
	}
	
	/**
	 * uc_electronic_seal明细页面
	 * @param id
	 * @return
	 * @throws Exception 
	 * ModelAndView
	 */
	@GetMapping(value="/getJson")
	@ApiOperation(value="uc_electronic_seal数据详情",httpMethod = "GET",notes = "uc_electronic_seal数据详情")
	public ElectronicSeal get(@ApiParam(name="id",value="业务对象主键", required = true)@RequestParam String id) throws Exception{
		return electronicSealManager.get(id);
	}
	
    /**
	 * 新增uc_electronic_seal
	 * @param electronicSeal
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@PostMapping(value="save")
	@ApiOperation(value = "添加签章成功", httpMethod = "POST", notes = "添加签章成功")
	public CommonResult<String> save(@ApiParam(name="electronicSeal",value="电子签章业务对象", required = true)@RequestBody ElectronicSeal electronicSeal) throws Exception{
		return electronicSealManager.saveElectronicSeal(electronicSeal);
	}

	/**
	 * front用户自己添加电子签章
	 * @param electronicSeal
	 * @return
	 */
	@PostMapping(value="userSave")
	@ApiOperation(value = "front用户自己添加电子签章", httpMethod = "POST", notes = "添加签章成功")
	public CommonResult<String> userSave(@ApiParam(name="electronicSeal",value="电子签章业务对象", required = true)@RequestBody ElectronicSeal electronicSeal) throws Exception {
		CommonResult<String> commonResult = electronicSealManager.userSaveElectronicSeal(electronicSeal);
		return CommonResultUtil.getResult(commonResult);
	}

	@PostMapping(value = "mobileUpdate")
	@ApiOperation(value = "手机端更新签章",httpMethod = "POST",notes = "手机端更新签章")
	public CommonResult<String> mobileUpdate(@RequestBody ElectronicSeal electronicSeal) throws Exception {
		CommonResult<String> commonResult = electronicSealManager.mobileUpdateElectronicSeal(electronicSeal);
		return CommonResultUtil.getResult(commonResult);
	}
	
	@RequestMapping(value="getElectronicSeal",method=RequestMethod.GET, produces = {"application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前登录用户的签章id", httpMethod = "GET", notes = "获取当前登录用户的签章id")
	public CommonResult<String> getUser(@ApiParam(name="password",value="签章使用密码") @RequestParam(required=true) Optional<String> password,@ApiParam(name="secretFree",value="是否免密") @RequestParam(required=true) Optional<Boolean> secretFree) throws Exception{
		return electronicSealManager.getUser(password,secretFree);
	}


	@GetMapping("/getSealByUserId/{userId}")
	@ApiOperation(value = "根据用户ID查询电子签章")
	public ElectronicSeal getSealByUserId(@PathVariable("userId") String userId){
		// 根据用户ID，查询出当前正在使用的电子签章
		ElectronicSeal electronicSeal = electronicSealManager.getOne(new QueryWrapper<ElectronicSeal>().eq("IS_USE_", 0).eq("USER_ID_", userId));
		if (BeanUtils.isEmpty(electronicSeal)){
			return null;
		}
		electronicSeal.setPassword("");
		return electronicSeal;
	}
	@GetMapping("/getSealByCurrentUserId")
	@ApiOperation(value = "获取当前登录用户的电子签章")
	public ElectronicSeal getCurrentUserSeal(){
		ElectronicSeal res=electronicSealManager.getCurrentUserSeal();
		return res;
	}
	/**
	 * 批量删除uc_electronic_seal记录
	 * @param ids
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@DeleteMapping(value="/remove")
	@ApiOperation(value = "批量删除uc_electronic_seal记录", httpMethod = "DELETE", notes = "批量删除uc_electronic_seal记录")
	public CommonResult<String> removes(@ApiParam(name="ids",value="业务主键数组,多个业务主键之间用逗号分隔", required = true)@RequestParam String...ids) throws Exception{
		electronicSealManager.removeByIds(ids);
		return new CommonResult<String>(true, "批量删除成功");
	}
	
	@RequestMapping(value="/saveSignatureBatch",method=RequestMethod.POST, produces = {
	"application/json; charset=utf-8" })
	@ApiOperation(value = "处理批量导入的用户签章数据", httpMethod = "POST", notes = "处理批量导入的用户签章数据")
	public CommonResult<String> saveSignatureBatch(@ApiParam(name="signatureData",value="导入的用户签章数据",required=true) @RequestBody ImportSignatureObject signatureData) throws Exception{
		return baseService.importSignature(signatureData);
	}
	
	/**
	 * 对签章密码进行加密
	 * @param blankPswd
	 * @return
	 * @throws Exception 
	 */
	private String encryptSealPassword(String blankPswd) throws Exception {
		return EncryptUtil.encrypt(blankPswd);
	}
}
