/*
 *    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.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDictItem;
import com.qunzhixinxi.hnqz.admin.entity.WmDaDistribution;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackage;
import com.qunzhixinxi.hnqz.admin.entity.WmScorePackageStatus;
import com.qunzhixinxi.hnqz.admin.entity.WmTask;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskContent;
import com.qunzhixinxi.hnqz.admin.mapper.WmDaDistributionMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmTaskContentMapper;
import com.qunzhixinxi.hnqz.admin.service.SysDictItemService;
import com.qunzhixinxi.hnqz.admin.service.WmDaDistributionService;
import com.qunzhixinxi.hnqz.admin.service.WmDaHospitalService;
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.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 档案-配送公司
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:42
 */
@Service
@AllArgsConstructor
public class WmDaDistributionServiceImpl extends ServiceImpl<WmDaDistributionMapper, WmDaDistribution> implements WmDaDistributionService {

	private final WmTaskContentMapper wmTaskContentMapper;
	private final WmDaDistributionMapper wmDaDistributionMapper;
	private final WmTaskService wmTaskService;
	private final SysDictItemService sysDictItemService;
	private final WmDaHospitalService wmDaHospitalService;

	private final WmScorePackageService packageService;
	private final WmScorePackageStatusService packageStatusService;

	@Override
	public boolean saveOrUpdateDistributionlByTask(WmTask wmTask) {
		WmTaskContent wmTaskContent = wmTaskContentMapper.selectById(wmTask);
		WmDaDistribution wmDaDistribution = new WmDaDistribution();
		wmDaDistribution.setShyshxydm(wmTaskContent.getTemp6());
		List<WmDaDistribution> list = this.list(Wrappers.query(wmDaDistribution));
		if (list.size() > 0) {
			wmDaDistribution = list.get(0);
		}

		wmDaDistribution.setGsmc(wmTaskContent.getTemp3());
		wmDaDistribution.setProvince(wmTaskContent.getTemp4().split(",")[0]);
		wmDaDistribution.setCity(wmTaskContent.getTemp4().split(",")[1]);
		wmDaDistribution.setArea(wmTaskContent.getTemp4().split(",")[2]);
		wmDaDistribution.setGsgm(wmTaskContent.getTemp5());
		wmDaDistribution.setShyshxydm(wmTaskContent.getTemp6());
		wmDaDistribution.setGszcd(wmTaskContent.getTemp7());
		wmDaDistribution.setZczb(wmTaskContent.getTemp8());
		wmDaDistribution.setGspzsbh(wmTaskContent.getTemp9());
		wmDaDistribution.setFrdb(wmTaskContent.getTemp10());
		wmDaDistribution.setZjl(wmTaskContent.getTemp11());
		wmDaDistribution.setCjbmfzrxm(wmTaskContent.getTemp12());
		wmDaDistribution.setCjbmfzrlxfs(wmTaskContent.getTemp13());
		wmDaDistribution.setCgbmdjrxm(wmTaskContent.getTemp14());
		wmDaDistribution.setCgbmdjrlxfs(wmTaskContent.getTemp15());
		wmDaDistribution.setXsbmfzrxm(wmTaskContent.getTemp16());
		wmDaDistribution.setXsbmfzrlxfs(wmTaskContent.getTemp17());
		wmDaDistribution.setXsbmdjrxm(wmTaskContent.getTemp18());
		wmDaDistribution.setXsbmdjrlxfs(wmTaskContent.getTemp19());
		wmDaDistribution.setGsjj(wmTaskContent.getTemp20());
		wmDaDistribution.setJyys(wmTaskContent.getTemp21());

		return this.saveOrUpdate(wmDaDistribution);
	}

	@Override
	public List<WmDaDistribution> listByPoint(WmDaDistribution wmDaDistribution) {
		return wmDaDistributionMapper.listByPoint(wmDaDistribution);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R approvalDistribution(String taskId) {
		List<Map<String ,String>> compareList=new ArrayList<>();
		WmDaDistribution wmDaDistribution=new WmDaDistribution();
		WmDaDistribution newinfo=new WmDaDistribution();//对比后数据
		//获取任务信息
		WmTask wmTask=wmTaskService.getById(taskId);
		if(null==wmTask){
			return R.failed("未查询到调研信息");
		}
		if("1".equals(wmTask.getPlatAuditStatus())){
			return R.failed("信息已审核，请重新查询后操作");
		}
		WmTaskContent wmTaskContent=wmTaskContentMapper.selectById(wmTask.getTaskContentId());
		if(null==wmTaskContent){
			return R.failed("调研信息为空");
		}

		//Temp29对应医院ID
		Boolean exist = false;
		if(StringUtils.isNotEmpty(wmTaskContent.getTemp29())){
			//有医院id,获取医院信息
			wmDaDistribution=this.getById(wmTaskContent.getTemp29());
			if(wmDaDistribution!=null){
				exist = true;
			}
		}

		if(!exist && StringUtils.isNotEmpty(wmTaskContent.getTemp3())){
			//getTemp3 商业公司  防止新增任务后未审核前 商业公司存在
			WmDaDistribution query=new WmDaDistribution();
			query.setDelFlag(0);
			query.setEnableFlag(0);
			query.setGsmc(wmTaskContent.getTemp3());
			List<WmDaDistribution> hospitals = this.list(Wrappers.query(query));
			if(CollectionUtils.isNotEmpty(hospitals)){
				wmDaDistribution = hospitals.get(0);
				exist = true;
			}
		}

		//Temp29对应医院ID
		if(exist){
			newinfo.setId(wmDaDistribution.getId());
			compareList=compareData(wmDaDistribution,wmTaskContent,newinfo);
			if(null!=compareList&&compareList.size()>0){
				wmTask.setCompareResult("2");//信息不一致
				wmTaskContent.setTemp29(wmDaDistribution.getId());
			}else{
				wmTask.setCompareResult("3");//信息一致
			}
		}else{
			wmTask.setCompareResult("1");//新增
			//如果是新增，需要匹配原来的字段值
			wmDaDistribution.setGsmc(wmTaskContent.getTemp3());//名称
			if(!StringUtil.isEmpty(wmTaskContent.getTemp4())){//省市区
				String[] citys=wmTaskContent.getTemp4().split("-");
				if(citys.length==3) {
					wmDaDistribution.setProvince(citys[0]);
					wmDaDistribution.setCity(citys[1]);
					wmDaDistribution.setArea(citys[2]);
				}else {
					String[] citys1=wmTaskContent.getTemp4().split(",");
					if(citys1.length==3) {
						wmDaDistribution.setProvince(citys1[0]);
						wmDaDistribution.setCity(citys1[1]);
						wmDaDistribution.setArea(citys1[2]);
					}
				}
			}
			wmDaDistribution.setAddress(wmTaskContent.getTemp4());
			if(null!=wmTaskContent.getTemp5()){
				//小程序端存的value,web存的label，需要匹配
				SysDictItem sysDictItemConn = new SysDictItem();
				sysDictItemConn.setType("ent_gm");
				List<SysDictItem> dictItems = sysDictItemService.list(new QueryWrapper<>(sysDictItemConn));

				Map<String, String> partsMap = dictItems.stream().collect(Collectors.toMap(k -> k.getLabel(), k->k.getValue()));
				String value=partsMap.get(wmTaskContent.getTemp5());
				wmDaDistribution.setGsgm(value);//公司规模
			}
			wmDaDistribution.setGsgm(wmTaskContent.getTemp5());//公司规模
			wmDaDistribution.setShyshxydm(wmTaskContent.getTemp6());//统一社会信用代码
			wmDaDistribution.setGszcd(wmTaskContent.getTemp7());//公司注册地
			wmDaDistribution.setZczb(wmTaskContent.getTemp8());//注册资本
			wmDaDistribution.setGspzsbh(wmTaskContent.getTemp9());//GSP证书编号
			wmDaDistribution.setFrdb(wmTaskContent.getTemp10());//法人代表
			wmDaDistribution.setZjl(wmTaskContent.getTemp11());//总经理
			wmDaDistribution.setCjbmfzrxm(wmTaskContent.getTemp12());//采购部门负责人姓名
			wmDaDistribution.setCjbmfzrlxfs(wmTaskContent.getTemp13());//采购部门负责人联系方式
			wmDaDistribution.setCgbmdjrxm(wmTaskContent.getTemp14());//采购部门对接人姓名
			wmDaDistribution.setCgbmdjrlxfs(wmTaskContent.getTemp15());//采购部门对接人联系方式
			wmDaDistribution.setXsbmfzrxm(wmTaskContent.getTemp16());//销售部门负责人姓名
			wmDaDistribution.setXsbmfzrlxfs(wmTaskContent.getTemp17());//销售部门负责人联系方式
			wmDaDistribution.setXsbmdjrxm(wmTaskContent.getTemp18());//销售部门对接人姓名
			wmDaDistribution.setXsbmdjrlxfs(wmTaskContent.getTemp19());//销售部门对接人联系方式
			wmDaDistribution.setGsjj(wmTaskContent.getTemp20());//公司简介
			wmDaDistribution.setJyys(wmTaskContent.getTemp21());//经营优势
			wmDaDistribution.setMedicineTraceability(wmTaskContent.getTemp22()); // 药品信息化追溯体系
			wmDaDistribution.setMedicineTraceabilityOther(wmTaskContent.getTemp25()); // 追溯体系介绍
			wmDaDistribution.setCreateTime(LocalDateTime.now());
			newinfo=wmDaDistribution;

		}
		Map<String,Object> result=new HashMap();
		result.put("compareList",compareList);//对比数据
		result.put("info",wmDaDistribution);//数据
		result.put("newinfo",newinfo);//数据
		result.put("wmTask",wmTask);
		result.put("wmTaskContent",wmTaskContent);

		return R.ok(result);
	}



	/**
	 * 对比系统数据，和调研数据
	 * @return
	 */
	public List<Map<String ,String>> compareData(WmDaDistribution wmDaDistribution, WmTaskContent wmTaskContent, WmDaDistribution newinfo){
		List<Map<String ,String>> resList=new ArrayList<>();

//		String address=wmDaDistribution.getProvince()+"-"+wmDaDistribution.getCity()+"-"+wmDaDistribution.getArea();
//		if(null==wmDaDistribution.getProvince()){
//			address=null;
//
//		}
//		if(null!=wmTaskContent.getTemp4()&&!wmTaskContent.getTemp4().equals(address)){
//			resList.add(generateMap("所在城市",address,wmTaskContent.getTemp4()));
//			newinfo.setAddress(wmTaskContent.getTemp4());
//			String[] citys=wmTaskContent.getTemp4().split("-");
//			if(citys.length==3) {
//				newinfo.setProvince(citys[0]);
//				newinfo.setCity(citys[1]);
//				newinfo.setArea(citys[2]);
//			}else {
//				String[] citys1=wmTaskContent.getTemp4().split(",");
//				if(citys1.length==3) {
//					newinfo.setProvince(citys1[0]);
//					newinfo.setCity(citys1[1]);
//					newinfo.setArea(citys1[2]);
//				}
//			}
//		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp4()) && !wmTaskContent.getTemp4().equals(wmDaDistribution.getSygsdz())){
			resList.add(generateMap("商业公司地址",wmDaDistribution.getSygsdz(),wmTaskContent.getTemp4()));
			newinfo.setSygsdz(wmTaskContent.getTemp4());
		}

		if (StringUtils.isNotBlank(wmTaskContent.getTemp3()) && !wmTaskContent.getTemp3().equals(wmDaDistribution.getGsmc())){
			resList.add(generateMap("商业公司名称",wmDaDistribution.getGsmc(),wmTaskContent.getTemp3()));
			newinfo.setGsmc(wmTaskContent.getTemp3());
		}

		if (StringUtils.isNotBlank(wmTaskContent.getTemp5())) {
			//小程序端存的不一定value或label,web存的label，需要匹配
			Map<String,String> map=wmDaHospitalService.compareDictItem("公司规模",wmDaDistribution.getGsgm(),wmTaskContent.getTemp5(),"bus_gsgm");
			if(null!=map&&!map.isEmpty()){
				resList.add(map);
				newinfo.setGsgm(map.get("newData"));
			}
			//小程序端存的不一定是label或name,web存的label，需要匹配
		}
//		if(null!=wmTaskContent.getTemp5()&&!wmTaskContent.getTemp5().equals(wmDaDistribution.getGsgm())){
//			resList.add(generateMap("公司规模",wmDaDistribution.getGsgm(),wmTaskContent.getTemp5()));
//			newinfo.setGsgm(wmTaskContent.getTemp5());
//		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp6()) && !wmTaskContent.getTemp6().equals(wmDaDistribution.getShyshxydm())){
			resList.add(generateMap("统一社会信用代码", wmDaDistribution.getShyshxydm(), wmTaskContent.getTemp6()));
			newinfo.setShyshxydm(wmTaskContent.getTemp6());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp7()) && !wmTaskContent.getTemp7().equals(wmDaDistribution.getGszcd())){
			resList.add(generateMap("公司注册地",wmDaDistribution.getGszcd(),wmTaskContent.getTemp7()));
			newinfo.setGszcd(wmTaskContent.getTemp7());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp8()) && !wmTaskContent.getTemp8().equals(wmDaDistribution.getZczb())){
			resList.add(generateMap("注册资本",wmDaDistribution.getZczb(),wmTaskContent.getTemp8()));
			newinfo.setZczb(wmTaskContent.getTemp8());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp9()) && !wmTaskContent.getTemp9().equals(wmDaDistribution.getGspzsbh())){
			resList.add(generateMap("GSP证书编号",wmDaDistribution.getGspzsbh(),wmTaskContent.getTemp9()));
			newinfo.setGspzsbh(wmTaskContent.getTemp9());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp10()) && !wmTaskContent.getTemp10().equals(wmDaDistribution.getFrdb())){
			resList.add(generateMap("法人代表",wmDaDistribution.getFrdb(),wmTaskContent.getTemp10()));
			newinfo.setFrdb(wmTaskContent.getTemp10());
		}
//		if(null!=wmTaskContent.getTemp11()&&!wmTaskContent.getTemp11().equals(wmDaDistribution.getZjl())){
//			resList.add(generateMap("总经理",wmDaDistribution.getZjl(),wmTaskContent.getTemp11()));
//			newinfo.setZjl(wmTaskContent.getTemp11());
//		}
//
//		if(null!=wmTaskContent.getTemp12()&&!wmTaskContent.getTemp12().equals(wmDaDistribution.getCjbmfzrxm())){
//			resList.add(generateMap("采购部门负责人姓名",wmDaDistribution.getCjbmfzrxm(),wmTaskContent.getTemp12()));
//			newinfo.setCjbmfzrxm(wmTaskContent.getTemp12());
//		}
//		if(null!=wmTaskContent.getTemp13()&&!wmTaskContent.getTemp13().equals(wmDaDistribution.getCjbmfzrlxfs())){
//			resList.add(generateMap("采购部门负责人联系方式",wmDaDistribution.getCjbmfzrlxfs(),wmTaskContent.getTemp13()));
//			newinfo.setCjbmfzrlxfs(wmTaskContent.getTemp13());
//		}
//		if(null!=wmTaskContent.getTemp14()&&!wmTaskContent.getTemp14().equals(wmDaDistribution.getCgbmdjrxm())){
//			resList.add(generateMap("采购部门对接人姓名",wmDaDistribution.getCgbmdjrxm(),wmTaskContent.getTemp14()));
//			newinfo.setCgbmdjrxm(wmTaskContent.getTemp14());
//		}
//		if(null!=wmTaskContent.getTemp15()&&!wmTaskContent.getTemp15().equals(wmDaDistribution.getCgbmdjrlxfs())){
//			resList.add(generateMap("采购部门对接人联系方式",wmDaDistribution.getCgbmdjrlxfs(),wmTaskContent.getTemp15()));
//			newinfo.setCgbmdjrlxfs(wmTaskContent.getTemp15());
//		}
//		if(null!=wmTaskContent.getTemp16()&&!wmTaskContent.getTemp16().equals(wmDaDistribution.getXsbmfzrxm())){
//			resList.add(generateMap("销售部门负责人姓名",wmDaDistribution.getXsbmfzrxm(),wmTaskContent.getTemp16()));
//			newinfo.setXsbmfzrxm(wmTaskContent.getTemp16());
//		}
//		if(null!=wmTaskContent.getTemp17()&&!wmTaskContent.getTemp17().equals(wmDaDistribution.getXsbmfzrlxfs())){
//			resList.add(generateMap("销售部门负责人联系方式",wmDaDistribution.getXsbmfzrlxfs(),wmTaskContent.getTemp17()));
//			newinfo.setXsbmfzrlxfs(wmTaskContent.getTemp17());
//		}
//		if(null!=wmTaskContent.getTemp18()&&!wmTaskContent.getTemp18().equals(wmDaDistribution.getXsbmdjrxm())){
//			resList.add(generateMap("销售部门对接人姓名",wmDaDistribution.getXsbmdjrxm(),wmTaskContent.getTemp18()));
//			newinfo.setXsbmdjrxm(wmTaskContent.getTemp18());
//		}
//		if(null!=wmTaskContent.getTemp19()&&!wmTaskContent.getTemp19().equals(wmDaDistribution.getXsbmdjrlxfs())){
//			resList.add(generateMap("销售部门对接人联系方式",wmDaDistribution.getXsbmdjrlxfs(),wmTaskContent.getTemp19()));
//			newinfo.setXsbmdjrlxfs(wmTaskContent.getTemp19());
//		}

		if (StringUtils.isNotBlank(wmTaskContent.getTemp20()) && !wmTaskContent.getTemp20().equals(wmDaDistribution.getGsjj())){
			resList.add(generateMap("公司简介",wmDaDistribution.getGsjj(),wmTaskContent.getTemp20()));
			newinfo.setGsjj(wmTaskContent.getTemp20());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp21()) && !wmTaskContent.getTemp21().equals(wmDaDistribution.getJyys())){
			resList.add(generateMap("经营优势",wmDaDistribution.getJyys(),wmTaskContent.getTemp21()));
			newinfo.setJyys(wmTaskContent.getTemp21());
		}
		if (StringUtils.isNotBlank(wmTaskContent.getTemp22()) && !wmTaskContent.getTemp22().equals(wmDaDistribution.getMedicineTraceability())) {
			resList.add(generateMap("药品信息化追溯体系", WmDaDistribution.resolve(wmDaDistribution.getMedicineTraceability()).getName(),
					WmDaDistribution.resolve(wmTaskContent.getTemp22()).getName()));
			newinfo.setMedicineTraceability(wmTaskContent.getTemp22());
		}
		if (StringUtils.isNotBlank( wmTaskContent.getTemp25()) && !wmTaskContent.getTemp25().equals(wmDaDistribution.getMedicineTraceabilityOther())) {
			resList.add(generateMap("追溯体系介绍", wmDaDistribution.getMedicineTraceabilityOther(), wmTaskContent.getTemp25()));
			newinfo.setMedicineTraceabilityOther(wmTaskContent.getTemp25());
		}

		return resList;

	}

	public Map<String,String> generateMap(String name,String oldData,String newData){
		Map map=new HashMap();
		map.put("name",name);
		map.put("oldData",oldData);
		map.put("newData",newData);
		return map;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public R updateByApproval(Map<String, String> map) {
		//先更新任务表
		String taskId=map.get("taskId");
		WmTask wmTask=wmTaskService.getById(taskId);
		if(wmTask==null){
			return R.failed("未查询到调查信息");
		}
		String approvalInfo=map.get("approvalInfo");
		String approvalOpinion=map.get("approvalOpinion");
		String compareResult=map.get("compareResult");

//		LambdaUpdateWrapper<WmTask> updateWrapper=new LambdaUpdateWrapper<WmTask>();
//		updateWrapper.set(WmTask::getPlatAuditStatus,"1");
//		updateWrapper.set(WmTask::getApprovalOpinion,approvalOpinion);
//		updateWrapper.set(WmTask::getCompareResult,compareResult);
//		updateWrapper.set(WmTask::getApprovalInfo,approvalInfo);
//		updateWrapper.eq(WmTask::getTaskContentId,wmTask.getTaskContentId());
//		updateWrapper.eq(WmTask::getCreateTime,wmTask.getCreateTime());

		WmTask updateTask = new WmTask();
		updateTask.setId(taskId);
		updateTask.setPlatAuditStatus("1");
		updateTask.setApprovalOpinion(approvalOpinion);
		updateTask.setCompareResult(compareResult);
		updateTask.setApprovalInfo(approvalInfo);

//		wmTask.setApprovalInfo(approvalInfo);
//		wmTask.setApprovalOpinion(approvalOpinion);
//		wmTask.setCompareResult(compareResult);
//		wmTask.setPlatAuditStatus("1");
//		wmTaskService.updateById(wmTask);
		WmDaDistribution wmDaDistribution=generateDistribution(map);

		if("1".equals(approvalOpinion)){//采纳
			// TODO 根据任务积分修改积分包状态
//			updateWrapper.set(WmTask::getRealFlag,"0");//0通过
			updateTask.setRealFlag("0");
			//更新审核信息
			LocalDateTime now = LocalDateTime.now();
			Integer userId = SecurityUtils.getUser().getId();
			if("1".equals(compareResult)){//新增
				//新增
				wmDaDistribution.setCreateTime(now);
				wmDaDistribution.setCreateUser(userId);
				this.save(wmDaDistribution);
			}else if("2".equals(compareResult)){//信息不一致
				wmDaDistribution.setUpdateTime(now);
				wmDaDistribution.setUpdateUser(userId);
				this.updateById(wmDaDistribution);
			}else if("3".equals(compareResult)){//信息一致
				//信息一致的情况下，只改地址
				if(!StringUtil.isEmpty(map.get("address"))){
					WmDaDistribution wmDaDistribution1=new WmDaDistribution();
					wmDaDistribution1.setId(wmDaDistribution.getId());
					wmDaDistribution1.setAddress(map.get("address"));
					wmDaDistribution1.setLongitude(map.get("longitude"));
					wmDaDistribution1.setLatitude(map.get("latitude"));
					wmDaDistribution1.setUpdateTime(now);
					wmDaDistribution1.setUpdateUser(userId);
					this.updateById(wmDaDistribution1);
				}
			}
			//更新任务内容表
			WmTaskContent wmTaskContent=wmTaskContentMapper.selectById(wmTask.getTaskContentId());
			String infoId=map.get("infoId");
			wmTaskContent.setTemp29(infoId);
			wmTaskContentMapper.updateById(wmTaskContent);
		}else{
			//updateWrapper.set(WmTask::getTaskStatus,"2");//审核不通过
//			updateWrapper.set(WmTask::getRealFlag,"1");//1不通过
			updateTask.setRealFlag("1");
		}

		//提交
		updateTask.setTaskStatus("2");
		WmScorePackage wmScorePackage = packageService.getById(wmTask.getScorePackageId());
// 		if("1".equals(approvalOpinion)){
// 			if("1".equals(wmScorePackage.getPackageType1())){
// 				Boolean fullFlag = wmTaskService.checkUserTaskSubScore(wmTask.getTaskUserId());
// 				if(fullFlag){
// //					return R.failed(1, "每日最多提交5万积分");
// 					updateTask.setScore(0);
// 				}
//
// 			}
// 		}

		wmTaskService.updateById(updateTask);

		//审核之后的操作//采纳
//		if("1".equals(approvalOpinion)){
//		}else {

			//更新积分包状态

			WmScorePackageStatus query = new WmScorePackageStatus();
			query.setUserId(wmTask.getTaskUserId());
			query.setPackageId(wmTask.getScorePackageId());
			query.setStatus("2");
			WmScorePackageStatus scorePackageStatus = packageStatusService.getOne(Wrappers.query(query));

			/**
			 * 不需要释放 直接返回
			 */
//			if("1".equals(wmScorePackage.getTaskAddFlag()) && "1".equals(scorePackageStatus.getTaskAddFlag())){
//				return R.ok();
//			}

			packageStatusService.updatePackageAndStatus(wmTask, wmScorePackage, scorePackageStatus);
//		}
		return R.ok();
	}



	public WmDaDistribution generateDistribution(Map<String,String> map){
		WmDaDistribution wmDaDistribution= BeanUtil.copyProperties(map,WmDaDistribution.class);
//		wmDaDistribution.setId(StringUtil.isEmpty(map.get("id"))?null:map.get("id"));
//		wmDaDistribution.setGsmc(StringUtil.isEmpty(map.get("gsmc"))?null:map.get("gsmc"));
//		wmDaDistribution.setProvince(StringUtil.isEmpty(map.get("province"))?null:map.get("province"));
//		wmDaDistribution.setCity(StringUtil.isEmpty(map.get("city"))?null:map.get("city"));
//		wmDaDistribution.setArea(StringUtil.isEmpty(map.get("area"))?null:map.get("area"));
//		wmDaDistribution.setAddress(StringUtil.isEmpty(map.get("address"))?null:map.get("address"));
//
//		wmDaDistribution.setGsgm(StringUtil.isEmpty(map.get("gsgm"))?null:map.get("gsgm"));//公司规模
//		wmDaDistribution.setShyshxydm(StringUtil.isEmpty(map.get("shyshxydm"))?null:map.get("shyshxydm"));//统一社会信用代码
//		wmDaDistribution.setGszcd(StringUtil.isEmpty(map.get("gszcd"))?null:map.get("gszcd"));//公司注册地
//		wmDaDistribution.setZczb(StringUtil.isEmpty(map.get("zczb"))?null:map.get("zczb"));//注册资本
//		wmDaDistribution.setGspzsbh(StringUtil.isEmpty(map.get("gspzsbh"))?null:map.get("gspzsbh"));//GSP证书编号
//		wmDaDistribution.setFrdb(StringUtil.isEmpty(map.get("frdb"))?null:map.get("frdb"));//法人代表
//		wmDaDistribution.setZjl(StringUtil.isEmpty(map.get("zjl"))?null:map.get("zjl"));//总经理
//		wmDaDistribution.setCjbmfzrxm(StringUtil.isEmpty(map.get("cjbmfzrxm"))?null:map.get("cjbmfzrxm"));//采购部门负责人姓名
//		wmDaDistribution.setCjbmfzrlxfs(StringUtil.isEmpty(map.get("cjbmfzrlxfs"))?null:map.get("cjbmfzrlxfs"));//采购部门负责人联系方式
//		wmDaDistribution.setCgbmdjrxm(StringUtil.isEmpty(map.get("cgbmdjrxm"))?null:map.get("cgbmdjrxm"));//采购部门对接人姓名
//		wmDaDistribution.setCgbmdjrlxfs(StringUtil.isEmpty(map.get("cgbmdjrlxfs"))?null:map.get("cgbmdjrlxfs"));//采购部门对接人联系方式
//		wmDaDistribution.setXsbmfzrxm(StringUtil.isEmpty(map.get("xsbmfzrxm"))?null:map.get("xsbmfzrxm"));//销售部门负责人姓名
//		wmDaDistribution.setXsbmfzrlxfs(StringUtil.isEmpty(map.get("xsbmfzrlxfs"))?null:map.get("xsbmfzrlxfs"));//销售部门负责人联系方式
//		wmDaDistribution.setXsbmdjrxm(StringUtil.isEmpty(map.get("xsbmdjrxm"))?null:map.get("xsbmdjrxm"));//销售部门对接人姓名
//		wmDaDistribution.setXsbmdjrlxfs(StringUtil.isEmpty(map.get("xsbmdjrlxfs"))?null:map.get("xsbmdjrlxfs"));//销售部门对接人联系方式
//		wmDaDistribution.setGsjj(StringUtil.isEmpty(map.get("gsjj"))?null:map.get("gsjj"));//公司简介
//		wmDaDistribution.setJyys(StringUtil.isEmpty(map.get("jyys"))?null:map.get("jyys"));//经营优势
//		wmDaDistribution.setMedicineTraceability(StringUtils.isEmpty(map.get("medicineTraceability")) ? null : map.get("medicineTraceability"));
//		wmDaDistribution.setMedicineTraceabilityOther(StringUtils.isEmpty(map.get("medicineTraceabilityOther")) ? null : map.get("medicineTraceabilityOther"));
//
//		wmDaDistribution.setLongitude(StringUtil.isEmpty(map.get("longitude"))?null:map.get("longitude"));//经度
//		wmDaDistribution.setLatitude(StringUtil.isEmpty(map.get("latitude"))?null:map.get("latitude"));//纬度

		return wmDaDistribution;
	}
}
