/*
 *    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.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.*;
import com.qunzhixinxi.hnqz.admin.service.WmTaskContentService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.service.WmDaPharmacyService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 档案-药店
 *
 * @author pigx code generator
 * @date 2020-09-23 21:17:34
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmdapharmacy")
@Api(value = "wmdapharmacy", tags = "档案-药店管理")
public class WmDaPharmacyController {

	private final WmDaPharmacyService wmDaPharmacyService;

	private final WmTaskService wmTaskService;

	private final WmTaskContentService wmTaskContentService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param wmDaPharmacy 档案-药店
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('admin_wmdapharmacy_view')")
	public R getWmDaPharmacyPage(Page page, WmDaPharmacy wmDaPharmacy) {

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

		return R.ok(wmDaPharmacyService.page(page, Wrappers.query(wmDaPharmacy)));
	}


	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param wmDaPharmacy 档案-医院
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/pageAudit")
	public R pageAudit(Page page, WmDaPharmacy wmDaPharmacy) {
		if (null != wmDaPharmacy.getUpdateUser()) {
			WmTask wmTask = wmTaskService.getById(wmDaPharmacy.getUpdateUser());
			if (null != wmTask) {
				WmTaskContent wmTaskContent = wmTaskContentService.getById(wmTask.getTaskContentId());
				if (null != wmTaskContent && null != wmTaskContent.getTemp3()) {
					wmDaPharmacy.setId(wmTaskContent.getTemp3());
				} else {
					wmDaPharmacy.setId("-1");
				}
			} else {
				wmDaPharmacy.setId("-1");
			}
			wmDaPharmacy.setUpdateUser(null);
		}

		return R.ok(wmDaPharmacyService.page(page, Wrappers.query(wmDaPharmacy)));
	}


	/**
	 * 通过id查询档案-药店
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('admin_wmdapharmacy_view')")
	public R getById(@PathVariable("id") Integer id) {
		return R.ok(wmDaPharmacyService.getById(id));
	}

	/**
	 * 新增档案-药店
	 *
	 * @param wmDaPharmacy 档案-药店
	 * @return R
	 */
	@ApiOperation(value = "新增档案-药店", notes = "新增档案-药店")
	@SysLog("新增档案-药店")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('admin_wmdapharmacy_add')")
	public R save(@RequestBody WmDaPharmacy wmDaPharmacy) {
		return R.ok(wmDaPharmacyService.save(wmDaPharmacy));
	}

	/**
	 * 修改档案-药店
	 *
	 * @param wmDaPharmacy 档案-药店
	 * @return R
	 */
	@ApiOperation(value = "修改档案-药店", notes = "修改档案-药店")
	@SysLog("修改档案-药店")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('admin_wmdapharmacy_edit')")
	public R updateById(@RequestBody WmDaPharmacy wmDaPharmacy) {


		WmDaPharmacy query = new WmDaPharmacy();
		query.setPharmacyName(wmDaPharmacy.getPharmacyName());
		List<WmDaPharmacy> list = wmDaPharmacyService.list(Wrappers.query(query));

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


		Integer hid = wmDaPharmacy.getUpdateUser();
		wmDaPharmacy.setUpdateUser(0);
		wmDaPharmacy.setDelFlag("0");
		wmDaPharmacy.setEnableFlag("0");
		wmDaPharmacyService.updateById(wmDaPharmacy);
		if (null != hid && 0 != hid) {
			WmTask wmTask = wmTaskService.getById(hid);
			if (null != wmTask) {
				WmTaskContent wmTaskContent = wmTaskContentService.getById(wmTask.getTaskContentId());
				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_wmdapharmacy_del')")
	public R removeById(@PathVariable Integer id) {
		return R.ok(wmDaPharmacyService.removeById(id));
	}

	/**
	 * 获取药企列表
	 */
	@GetMapping("/listWmDaPharmacyByName")
	public R listWmDaPharmacyByName(Page page, String name) {
		WmDaPharmacy wmDaHospital = new WmDaPharmacy();
		if (null != name) {
			return R.ok(wmDaPharmacyService.page(page, Wrappers.query(wmDaHospital).like(true, "pharmacy_name", name)));

		} else {
			return R.ok(wmDaPharmacyService.page(page, Wrappers.query(wmDaHospital)));
		}
	}

	/**
	 * 根据名字获取药店列表
	 */
	@GetMapping("/getWmDaPharmacyByName")
	public R getWmDaPharmacyByName(String name) {
		WmDaPharmacy wmDaHospital = new WmDaPharmacy();
		if (StringUtils.isNotEmpty(name)) {
			wmDaHospital.setPharmacyName(name);
			List<WmDaPharmacy> pharmacyList = wmDaPharmacyService.list(Wrappers.query(wmDaHospital));
			if (CollectionUtils.isNotEmpty(pharmacyList)) {
				return R.ok(pharmacyList.get(0));
			}
		}
		return R.ok();
	}

	/**
	 * 批量转换经纬度
	 *
	 * @return
	 */
	@GetMapping("/parseAddress")
	public R<?> parseAddress() {
		wmDaPharmacyService.parseAddress();
		return R.ok();
	}

	/**
	 * 调查信息审核--药企
	 *
	 * @return
	 */
	@ApiOperation(value = "调查信息审核", notes = "调查信息审核")
	@GetMapping("/approvalPharmacy")
	public R approvalPharmacy(String taskId) {
		return wmDaPharmacyService.approvalPharmacy(taskId);
	}

	/**
	 * 保存审核意见--药企
	 *
	 * @return R
	 */
	@ApiOperation(value = "保存审核意见", notes = "保存审核意见")
	@SysLog("保存审核意见")
	@PostMapping("/updateByApproval")
	public R updateByApproval(@RequestBody Map<String, String> map) {

		return wmDaPharmacyService.updateByapproval(map);
	}

}
