/*
 *    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.conditions.query.QueryWrapper;
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.WmDaDrugEnt;
import com.qunzhixinxi.hnqz.admin.entity.WmDaPharmacy;
import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.entity.WmDaDrugEntDrugtable;
import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntDrugtableService;
import com.qunzhixinxi.hnqz.common.security.annotation.Inner;
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.time.LocalDateTime;
import java.util.List;


/**
 * 档案-药企药品
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:42
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmdadrugentdrugtable")
@Api(value = "wmdadrugentdrugtable", tags = "档案-药企药品管理")
public class WmDaDrugEntDrugtableController {

	private final WmDaDrugEntDrugtableService wmDaDrugEntDrugtableService;
	private final WmDaDrugEntService wmDaDrugEntService;

	/**
	 * 分页查询
	 *
	 * @param page                 分页对象
	 * @param wmDaDrugEntDrugtable 档案-药企药品
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('admin_wmdadrugentdrugtable_view')")
	public R getWmDaDrugEntDrugtablePage(Page page, WmDaDrugEntDrugtable wmDaDrugEntDrugtable) {
		OrderItem orderItem = new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);
		QueryWrapper queryWrapper = Wrappers.query(wmDaDrugEntDrugtable);
		String drugname = wmDaDrugEntDrugtable.getDrugname();
		if (StringUtils.isNotEmpty(wmDaDrugEntDrugtable.getDrugname())) {
			wmDaDrugEntDrugtable.setDrugname(null);
			queryWrapper.like("drugname", drugname);
		} else {
			wmDaDrugEntDrugtable.setDrugname(null);
		}

		String drugnameTy = wmDaDrugEntDrugtable.getDrugnameTy();
		if (StringUtils.isNotEmpty(wmDaDrugEntDrugtable.getDrugnameTy())) {
			wmDaDrugEntDrugtable.setDrugnameTy(null);
			queryWrapper.like("drugname_ty", drugnameTy);
		} else {
			wmDaDrugEntDrugtable.setDrugnameTy(null);
		}
		if (StringUtils.isEmpty(wmDaDrugEntDrugtable.getDrugEntName())) {
			wmDaDrugEntDrugtable.setDrugEntName(null);
		}
		return R.ok(wmDaDrugEntDrugtableService.page(page, queryWrapper));
	}


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

	/**
	 * 新增档案-药企药品
	 *
	 * @param wmDaDrugEntDrugtable 档案-药企药品
	 * @return R
	 */
	@ApiOperation(value = "新增档案-药企药品", notes = "新增档案-药企药品")
	@SysLog("新增档案-药企药品")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('admin_wmdadrugentdrugtable_add')")
	public R save(@RequestBody WmDaDrugEntDrugtable wmDaDrugEntDrugtable) {

		WmDaDrugEntDrugtable query = new WmDaDrugEntDrugtable();
		query.setDrugname(wmDaDrugEntDrugtable.getDrugname());
		List<WmDaDrugEntDrugtable> list = wmDaDrugEntDrugtableService.list(Wrappers.query(query));
		if (list.size() > 0) {
			return R.failed("药品名称已存在");
		}
		WmDaDrugEnt drugEnt = new WmDaDrugEnt();
		List<WmDaDrugEnt> daDrugEnts = wmDaDrugEntService.list(Wrappers.query(drugEnt));
		if (CollectionUtils.isNotEmpty(daDrugEnts)) {
			wmDaDrugEntDrugtable.setDrugEntId(daDrugEnts.get(0).getId());
		}


		wmDaDrugEntDrugtable.setCreateTime(LocalDateTime.now());
		return R.ok(wmDaDrugEntDrugtableService.save(wmDaDrugEntDrugtable));
	}

	/**
	 * 修改档案-药企药品
	 *
	 * @param wmDaDrugEntDrugtable 档案-药企药品
	 * @return R
	 */
	@ApiOperation(value = "修改档案-药企药品", notes = "修改档案-药企药品")
	@SysLog("修改档案-药企药品")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('admin_wmdadrugentdrugtable_edit')")
	public R updateById(@RequestBody WmDaDrugEntDrugtable wmDaDrugEntDrugtable) {

		WmDaDrugEntDrugtable oldData = wmDaDrugEntDrugtableService.getById(wmDaDrugEntDrugtable.getId());
		if (!oldData.getDrugEntName().equals(wmDaDrugEntDrugtable.getDrugEntName())) {
			WmDaDrugEnt drugEnt = new WmDaDrugEnt();
			List<WmDaDrugEnt> daDrugEnts = wmDaDrugEntService.list(Wrappers.query(drugEnt));
			if (CollectionUtils.isNotEmpty(daDrugEnts)) {
				wmDaDrugEntDrugtable.setDrugEntId(daDrugEnts.get(0).getId());
			}
		}

//		if(!oldData.getDrugname().equals(wmDaDrugEntDrugtable.getDrugname())){
//			WmDaDrugEntDrugtable query = new WmDaDrugEntDrugtable();
//			query.setDrugname(wmDaDrugEntDrugtable.getDrugname());
//			List<WmDaDrugEntDrugtable> list = wmDaDrugEntDrugtableService.list(Wrappers.query(query));
//			if (list.size() > 0) {
//				return R.failed("药品名称已存在");
//			}
//		}


		return R.ok(wmDaDrugEntDrugtableService.updateById(wmDaDrugEntDrugtable));
	}

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


	@GetMapping("/list")
	public R listWmDaDrugEntDrugtable() {
		WmDaDrugEntDrugtable wmDaDrugEnt = new WmDaDrugEntDrugtable();
		wmDaDrugEnt.setDelFlag("0");
		wmDaDrugEnt.setEnableFlag("0");
		return R.ok(wmDaDrugEntDrugtableService.list(Wrappers.query(wmDaDrugEnt)));
	}


	@ApiOperation(value = "新增档案-药企药品", notes = "新增档案-药企药品")
	@SysLog("新增档案-药企药品")
	@PostMapping("/distribute")
	public R distribute(@RequestBody WmDaDrugEntDrugtable wmDaDrugEntDrugtable) {
		return wmDaDrugEntDrugtableService.distribute(wmDaDrugEntDrugtable);
	}

	@SysLog("获取药品关联部门列表")
	@GetMapping("/listReDept")
	public R listReDept(WmDaDrugEntDrugtable wmDaDrugEntDrugtable) {
		return wmDaDrugEntDrugtableService.listReDept(wmDaDrugEntDrugtable);
	}

	/**
	 * 获取药品生产企业名称列表
	 *
	 * @param deptId 部门id
	 * @return 名称列表
	 */
	@GetMapping(value = "/list-drug-producer")
	public R listDrugProducerByDeptId(@RequestParam(value = "deptId", required = false) Integer deptId) {
		return R.ok(wmDaDrugEntDrugtableService.listDrugProducerByDeptId(deptId));
	}

}
