package com.dxhy.config.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.config.sys.SysGroupConfigEntity;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.jxpt.baseService.config.service.SysGroupConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

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


/**
 * SysGroupConfig
 * <p>Title:SysGroupConfigController </p>
 * <p>Description:SysGroupConfig </p>
 * <p>Company: </p> 
 * @author wangruwei
 * @date 2023-06-21 10:05:44
 */
@Controller
@RequestMapping(value = "/sys/sysGroupConfig")
@Slf4j
public class SysGroupConfigController extends AbstractController {
	
	@Autowired
	private SysGroupConfigService sysGroupConfigService;

	/**
	 * 初始化集团配置表保存，更新接口
	 */
	@RequestMapping("/initUpdate")
	public ResponseEntity<?> list(@RequestBody Map<String,String> params) {
		//请求参数转换为实体
		SysGroupConfigEntity sysGroupConfig = getData(params);
		log.info("初始化集团配置表保存，更新接口入参:{}",JSON.toJSONString(sysGroupConfig));
		if(StringUtils.isEmpty(sysGroupConfig.getCompany())){
			return ResponseEntity.ok(R.error("必须选择一个集团"));
		}
		if(StringUtils.isEmpty(sysGroupConfig.getBusinessEnable())){
			return ResponseEntity.ok(R.error("是否启用业务单元必须选择"));
		}
		if("1".equals(sysGroupConfig.getBusinessEnable())&&StringUtils.isEmpty(sysGroupConfig.getBusinessType())){
			return ResponseEntity.ok(R.error("是否启用业务单元启用时业务单元取值必须选择"));
		}
		if(StringUtils.isEmpty(sysGroupConfig.getOcrType())){
			return ResponseEntity.ok(R.error("ocr识别渠道必须选择"));
		}
		if(StringUtils.isEmpty(sysGroupConfig.getQsStatus())){
			return ResponseEntity.ok(R.error("签收认证状态必须选择"));
		}
		if(StringUtils.isEmpty(sysGroupConfig.getQsStatus())){
			return ResponseEntity.ok(R.error("签收认证状态必须选择"));
		}
		if(StringUtils.isEmpty(sysGroupConfig.getIsCollext())){
			return ResponseEntity.ok(R.error("是否采集全票面必须选择"));
		}

		sysGroupConfigService.updateSysGroupConfigService(sysGroupConfig,getUserInfo());

		return ResponseEntity.ok(R.ok());

		/*//有id则更新，无id是新增
		if(ObjectUtil.isEmpty(sysGroupConfig.getId())){
			//新增

		}else{
			//修改
			sysGroupConfigService.createSysGroupConfigService(sysGroupConfig);
		}*/


	}

	private SysGroupConfigEntity getData(Map<String, String> params) {
		Date firstCollectDate = null;
		if(StringUtils.isNotEmpty(params.get("firstCollectDate"))){
			firstCollectDate = DateUtils.strToDate(params.get("firstCollectDate"), "yyyy-MM");
			params.put("firstCollectDate", "");
		}
		SysGroupConfigEntity sysGroupConfig = JSON.parseObject(JSON.toJSONString(params), SysGroupConfigEntity.class);
		sysGroupConfig.setFirstCollectDate(firstCollectDate);
		return sysGroupConfig;
	}

	/**
	* @Description 根据集团编码获取集团配置
	* @param params
	* @Return org.springframework.http.ResponseEntity<?>
	* @Author wangruwei
	* @Date 2023/6/26 16:57
	**/
	@RequestMapping("/getGroupInfo")
	public ResponseEntity<?> getGroupInfo(@RequestBody Map<String, String> params) {
		String company = params.get("company");
		if (StringUtils.isBlank(company)) {
			company = "ZXQY";
		}
		SysGroupConfigEntity sysGroupConfig = sysGroupConfigService.getGroupInfo(company);
		return ResponseEntity.ok(R.ok().put("data", sysGroupConfig));
	}

}

