package com.dxhy.config.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.jxpt.baseService.config.service.SysEnterpriseConfigService;
import com.dxhy.jxpt.baseService.config.service.SysLqZldwConfigService;
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.util.CollectionUtils;
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.ResponseBody;

import java.text.ParseException;
import java.util.List;
import java.util.Map;


/**
 * <p>Title:SysEnterpriseConfigController </p>
 * <p>Description:SysEnterpriseConfig </p>
 * <p>Company: </p> 
 * @author wangruwei
 * @date 2023-06-21 10:47:36
 */
@Controller
@Slf4j
@RequestMapping(value = "/sys/enterpriseConfig")
public class SysEnterpriseConfigController extends AbstractController {
	
	@Autowired
	private SysEnterpriseConfigService sysEnterpriseConfigService;
	@Autowired
	private SysLqZldwConfigService sysLqZldwConfigService;

	/**
	* @Description 企业信息列表
	* @param params
	* @Return org.springframework.http.ResponseEntity<?>
	* @Author wangruwei
	* @Date 2023/6/26 17:10
	**/
	@RequestMapping("/list")
	public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
		/**
		* @Description 只用到 纳税人识别号,纳税人名称,  接入方式：0大企业，1乐企   三个字段
		* @param sysEnterpriseConfig
		* ResponseEntity<?>
		* @Author wangruwei
		* @Date 2023/6/26 15:38
		**/
		PageUtils page = sysEnterpriseConfigService.queryPage(params);

		return ResponseEntity.ok(R.ok().put("page", page));
	}

	/**
	 * @Description 新增或修改企业信息详情
	 * @param sysEnterpriseConfig
	 * @Return org.springframework.http.ResponseEntity<?>
	 * @Author wangruwei
	 * @Date 2023/6/26 17:10
	 **/
	@RequestMapping("/updateEnterpriseInfo")
	public ResponseEntity<?> updateEnterpriseInfo(@RequestBody SysEnterpriseConfigEntity sysEnterpriseConfig) throws ParseException {

		if(StringUtils.isEmpty(sysEnterpriseConfig.getNsrsbh())){
			return ResponseEntity.ok(R.error("企业必须选择"));
		}
		if(StringUtils.isEmpty(sysEnterpriseConfig.getCompany())){
			return ResponseEntity.ok(R.error("集团编码必填"));
		}
		if(StringUtils.isEmpty(sysEnterpriseConfig.getJrfs())){
			return ResponseEntity.ok(R.error("接入方式必须选择"));
		}
		if("0".equals(sysEnterpriseConfig.getJrfs())){
			//大企业
			if(StringUtils.isEmpty(sysEnterpriseConfig.getSfcy())){
				return ResponseEntity.ok(R.error("是否查验必须选择"));
			}
			if(StringUtils.isEmpty(sysEnterpriseConfig.getSfcj())){
				return ResponseEntity.ok(R.error("是否采集必须选择"));
			}
		}else if("1".equals(sysEnterpriseConfig.getJrfs())){
			//乐企
			if(StringUtils.isEmpty(sysEnterpriseConfig.getSydwptbm())){
				return ResponseEntity.ok(R.error("使用单位平台编码必填"));
			}
		}

		String msg = sysEnterpriseConfigService.updateEnterpriseInfo(sysEnterpriseConfig, this.getUserInfo());
		if(StringUtils.isNotEmpty(msg)){
			return ResponseEntity.ok(R.error(msg));
		}else{
			return ResponseEntity.ok(R.ok().put("data", sysEnterpriseConfig));
		}
	}

	/**
	 * @Description 通过税号获取企业信息
	 * @param params  传company,nsrsbh
	 * @Return org.springframework.http.ResponseEntity<?>
	 * @Author wangruwei
	 * @Date 2023/6/26 17:10
	 **/
	@RequestMapping("/getEnterpriseInfoByNsrsbh")
	public ResponseEntity<?> getEnterpriseInfoByNsrsbh(@RequestBody Map<String, String> params) {
		if(StringUtils.isEmpty(params.get("company"))){
			return ResponseEntity.ok(R.error("集团编码必传"));
		}
		if(StringUtils.isEmpty(params.get("nsrsbh"))){
			return ResponseEntity.ok(R.error("税号必传"));
		}
		SysEnterpriseConfigEntity sysEnterpriseConfig = sysEnterpriseConfigService.getEnterpriseInfoByNsrsbh(params);

		return ResponseEntity.ok(R.ok().put("data", sysEnterpriseConfig));
	}

	/**
	* @Description 获取企业信息详情
	* @param params
	* @Return org.springframework.http.ResponseEntity<?>
	* @Author wangruwei
	* @Date 2023/6/26 17:10
	**/
	@RequestMapping("/getEnterpriseInfo")
	public ResponseEntity<?> getEnterpriseInfo(@RequestBody Map<String, String> params) {
		String id = params.get("id");
		if(StringUtils.isEmpty(id)){
			return ResponseEntity.ok(R.error("企业信息id必传"));
		}
		SysEnterpriseConfigEntity sysEnterpriseConfig = sysEnterpriseConfigService.selectEnterpriseInfo(id);

		return ResponseEntity.ok(R.ok().put("data", sysEnterpriseConfig));
	}

	/**
	 * @Description 删除企业信息
	 * @param params
	 * @Return org.springframework.http.ResponseEntity<?>
	 * @Author wangruwei
	 * @Date 2023/6/26 17:10
	 **/
	@RequestMapping("/delEnterprise")
	public ResponseEntity<?> delEnterprise(@RequestBody Map<String, String> params) {
		String id = params.get("id");
		if(StringUtils.isEmpty(id)){
			return ResponseEntity.ok(R.error("企业信息id必传"));
		}
		int num = sysEnterpriseConfigService.delEnterprise(id);
		return ResponseEntity.ok(R.ok());
	}

	/**
	 * @Description 获取集团是否已有大企业，用于判断有大企业后，aceid和acekey必须填
	 * @param params
	 * @Return org.springframework.http.ResponseEntity<?>
	 * @Author wangruwei
	 * @Date 2023/6/26 17:10
	 **/
	@RequestMapping("/checkEnterpriseDqy")
	public ResponseEntity<?> checkEnterpriseDqy(@RequestBody Map<String, String> params) {
		String company = params.get("company");
		if (StringUtils.isBlank(company)) {
			company = "ZXQY";
		}

		Long size = sysEnterpriseConfigService.checkEnterpriseDqy(company);
		return ResponseEntity.ok(R.ok().put("data", size));
	}

    /**
    * @Description 乐企或大企业导入功能
    * @param postImportDetail
    * @Return org.springframework.http.ResponseEntity<?>
    * @Author wangruwei
    * @Date 2023/6/28 14:37
    **/
	@PostMapping("/excel/importLqDqy")
	@ResponseBody
	@SysLog("导入签收")
	public ResponseEntity<?> importLqDqy(@RequestBody String postImportDetail) {
		try {

			JSONObject inportDetail = JSONObject.parseObject(postImportDetail);
			if (!inportDetail.containsKey("zsl") || StringHelper.isBlank(inportDetail.getString("zsl"))) {
				return ResponseEntity.ok(R.error("导入数据总数不能为空！"));
			}

			String jrfs = null;
			if (inportDetail.containsKey("jrfs")) {
				jrfs = inportDetail.getString("jrfs");
			}else{
				return ResponseEntity.ok(R.error("接入方式必选！"));
			}

			JSONArray detailArray = new JSONArray();
			//大企业
			if ("0".equals(jrfs)) {
				if(inportDetail.getJSONArray("dqyInfo").size()==0){
					return ResponseEntity.ok(R.error("大企业的企业信息不能为空！"));
				}else{
					detailArray = inportDetail.getJSONArray("dqyInfo");
				}
			}else if ("1".equals(jrfs)) {
				if(inportDetail.getJSONArray("lqInfo").size()==0){
					return ResponseEntity.ok(R.error("乐企的企业信息不能为空！"));
				}else{
					detailArray = inportDetail.getJSONArray("lqInfo");
				}
			}else{
				return ResponseEntity.ok(R.error("没有此接入方式！"));
			}

			if(detailArray.size()!=inportDetail.getLongValue("zsl")){
				return ResponseEntity.ok(R.error("企业信息数量和总数量不一致！"));
			}


			String company = inportDetail.getString("company");
			if(StringUtils.isEmpty(company)){
				return ResponseEntity.ok(R.error("集团编码必传！"));
			}

			List<String> msgList = sysEnterpriseConfigService.importEnterprise(company,jrfs,detailArray,getUserInfo());
			if (CollectionUtils.isEmpty(msgList)) {
				return ResponseEntity.ok(R.ok());
			}else{
				log.info("验证不通过:{}", JSON.toJSONString(msgList));
				return ResponseEntity.ok(R.error(9998, "验证不通过").put("errmsg", msgList));
			}
		} catch (Exception e) {
			log.error(CommonConstants.MSG_ERR_DEFAULT+"{}", e);
			return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
		}
	}


	/**
	 * @Description 获取直连单位信息
	 * @param params
	 * @Return org.springframework.http.ResponseEntity<?>
	 * @Author wangruwei
	 * @Date 2023/6/26 17:10
	 **/
	@RequestMapping("/listLqZldw")
	public ResponseEntity<?> listLqZldw(@RequestBody Map<String, String> params) {
		return ResponseEntity.ok(R.ok().put("data", sysLqZldwConfigService.listLqZldw()));
	}
	
}

