/*
 *    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.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.entity.WmDaHospital;
import com.qunzhixinxi.hnqz.admin.entity.WmTask;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskContent;
import com.qunzhixinxi.hnqz.admin.service.WmDaHospitalService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskContentService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
import com.qunzhixinxi.hnqz.admin.util.MapUtil;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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


/**
 * 档案-医院
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:42
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmdahospital" )
@Api(value = "wmdahospital", tags = "档案-医院管理")
public class WmDaHospitalController {

    private final  WmDaHospitalService wmDaHospitalService;

	private final WmTaskService wmTaskService;

	private final WmTaskContentService wmTaskContentService;

	/**
     * 分页查询
     * @param page 分页对象
     * @param wmDaHospital 档案-医院
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_wmdahospital_view')" )
    public R getWmDaHospitalPage(Page page, WmDaHospital wmDaHospital) {
    	String hospital=wmDaHospital.getYymc();

		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

    	if(null!=hospital)
		{
			wmDaHospital.setYymc(null);
			return R.ok(wmDaHospitalService.page(page, Wrappers.query(wmDaHospital).lambda().like(WmDaHospital::getYymc,hospital)));

		}
		return R.ok(wmDaHospitalService.page(page, Wrappers.query(wmDaHospital)));

	}


	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param wmDaHospital 档案-医院
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/pageAudit")
	public R pageAudit(Page page, WmDaHospital wmDaHospital) {
		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

		if(null!=wmDaHospital.getUpdateUser())
		{
			WmTask wmTask=wmTaskService.getById(wmDaHospital.getUpdateUser());
			if(null!=wmTask)
			{
				WmTaskContent wmTaskContent=wmTaskContentService.getById(wmTask.getTaskContentId());
				if(null!=wmTaskContent&&null!=wmTaskContent.getTemp29())
				{
					wmDaHospital.setId(wmTaskContent.getTemp29());
				}else{
					wmDaHospital.setId("-1");
				}
			}
			wmDaHospital.setUpdateUser(null);
		}

		return R.ok(wmDaHospitalService.page(page, Wrappers.query(wmDaHospital)));
	}


	/**
	 * 调查信息审核--医院
	 * @return
	 */
	@ApiOperation(value = "调查信息审核", notes = "调查信息审核")
	@GetMapping("/approvalHospital")
	public R approvalHospital(String taskId) {
		return wmDaHospitalService.approvalHospital(taskId);
	}

	/**
	 * 审核意见保存
	 * @return
	 */
	@ApiOperation(value = "审核意见保存", notes = "审核意见保存")
	@PostMapping("/updateByapproval")
	public R updateByapproval(@RequestBody Map<String,String> map) {
		return wmDaHospitalService.updateByApproval(map);

	}




    /**
     * 通过id查询档案-医院
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(wmDaHospitalService.getById(id));
    }

    /**
     * 新增档案-医院
     * @param wmDaHospital 档案-医院
     * @return R
     */
    @ApiOperation(value = "新增档案-医院", notes = "新增档案-医院")
    @SysLog("新增档案-医院" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_wmdahospital_add')" )
    public R save(@RequestBody WmDaHospital wmDaHospital) {

		WmDaHospital query=new WmDaHospital();
		query.setYymc(wmDaHospital.getYymc());
		List<WmDaHospital> list=wmDaHospitalService.list(Wrappers.query(query));

		if(list.size()>0)
		{
			return R.failed("医院名称已存在");
		}

		wmDaHospital.setCreateTime(LocalDateTime.now());
		Integer hid=wmDaHospital.getUpdateUser();
		wmDaHospital.setUpdateUser(0);
		wmDaHospital.setDelFlag("0");
		wmDaHospital.setEnableFlag("0");
		wmDaHospitalService.save(wmDaHospital);
		if(null!=hid&&0!=hid)
		{
			WmTask wmTask=wmTaskService.getById(hid);
			if(null!=wmTask)
			{
				WmTaskContent wmTaskContent=wmTaskContentService.getById(wmTask.getTaskContentId());
				if(null!=wmTaskContent)
				{
					wmTaskContent.setTemp29(wmDaHospital.getId());
					wmTaskContentService.updateById(wmTaskContent);
				}
				wmTask.setPlatAuditStatus("1");
				wmTaskService.updateById(wmTask);
			}
		}
        return R.ok();
    }

    /**
     * 修改档案-医院
     * @param wmDaHospital 档案-医院
     * @return R
     */
    @ApiOperation(value = "修改档案-医院", notes = "修改档案-医院")
    @SysLog("修改档案-医院" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_wmdahospital_edit')" )
    public R updateById(@RequestBody WmDaHospital wmDaHospital) {

		Integer hid=wmDaHospital.getUpdateUser();
		wmDaHospital.setUpdateUser(0);
		wmDaHospitalService.updateById(wmDaHospital);
		if(null!=hid&&0!=hid)
		{
			WmTask wmTask=wmTaskService.getById(hid);
			if(null!=wmTask)
			{
				WmTaskContent wmTaskContent=wmTaskContentService.getById(wmTask.getTaskContentId());
				if(null!=wmTaskContent)
				{
					wmTaskContent.setTemp29(wmDaHospital.getId());
					wmTaskContentService.updateById(wmTaskContent);
				}
				wmTask.setPlatAuditStatus("1");
				wmTaskService.updateById(wmTask);
			}
		}

        return R.ok();
    }

    /**
     * 通过id删除档案-医院
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除档案-医院", notes = "通过id删除档案-医院")
    @SysLog("通过id删除档案-医院" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_wmdahospital_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(wmDaHospitalService.removeById(id));
    }

	/**
	 * 获取药企列表
	 */
	@GetMapping("/listHospitalByName")
	public R listHospitalByName(Page page,String name) {
		WmDaHospital wmDaHospital=new WmDaHospital();
		wmDaHospital.setDelFlag("0");
		wmDaHospital.setEnableFlag("0");
		if(null!=name)
		{
			return R.ok(wmDaHospitalService.page(page,Wrappers.query(wmDaHospital).like(true,"yymc",name)));
		}else{
			return R.ok(wmDaHospitalService.page(page));
		}
	}

	/**
	 * 根据名称获取药企
	 */
	@GetMapping("/getHospitalByName")
	public R getHospitalByName(String name) {
		WmDaHospital wmDaHospital=new WmDaHospital();
		wmDaHospital.setDelFlag("0");
		wmDaHospital.setEnableFlag("0");
		if(StringUtils.isNotEmpty(name)){
			wmDaHospital.setYymc(name);
			List<WmDaHospital> hospitals = wmDaHospitalService.list(Wrappers.query(wmDaHospital));
			if(CollectionUtils.isNotEmpty(hospitals)){
				return R.ok(hospitals.get(0));
			}

		}
		return R.ok();
	}

	/**
	 * 获取药企列表
	 * @return 角色列表
	 */
	@GetMapping("/parseAddress")
	public R parseAddress() {
		LambdaQueryWrapper<WmDaHospital> wmDaHospitalLambdaQueryWrapper=Wrappers.lambdaQuery();

		wmDaHospitalLambdaQueryWrapper.isNull(WmDaHospital::getLatitude);

		List<WmDaHospital> wmDaHospitalList=wmDaHospitalService.list(wmDaHospitalLambdaQueryWrapper);

		if(wmDaHospitalList.size()>0)
		{
			for (WmDaHospital wmDaHospital:wmDaHospitalList) {
				JSONObject jsonObject= MapUtil.getLatAndLogByName(wmDaHospital.getAddress());
				if(null==jsonObject)
				{
					continue;
				}
				wmDaHospital.setProvince(jsonObject.getString("province"));
				wmDaHospital.setCity(jsonObject.getString("city"));
				wmDaHospital.setArea(jsonObject.getString("district"));
				String location=jsonObject.getString("location");
				if(null!=location&&location.split(",").length>0)
				{
					wmDaHospital.setLongitude(location.split(",")[0]);
					wmDaHospital.setLatitude(location.split(",")[1]);
				}

				wmDaHospitalService.saveOrUpdate(wmDaHospital);
			}
		}

		return R.ok();
	}
}
