/*
 *    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.WmDaDrugEntDrugtable;
import com.qunzhixinxi.hnqz.admin.service.WmDaDrugEntDrugtableService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.admin.entity.WmWkAlbum;
import com.qunzhixinxi.hnqz.admin.service.WmWkAlbumService;
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.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 文库-相册
 *
 * @author gaoyanng
 * @date 2020-06-25 23:35:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wmwkalbum" )
@Api(value = "wmwkalbum", tags = "文库-相册管理")
public class WmWkAlbumController {

    private final  WmWkAlbumService wmWkAlbumService;
	private final WmDaDrugEntDrugtableService wmDaDrugEntDrugtableService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wmWkAlbum 文库-相册
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_wmwkalbum_view')" )
    public R getWmWkAlbumPage(Page page, WmWkAlbum wmWkAlbum) {
		OrderItem orderItem=new OrderItem();
		orderItem.setColumn("create_time");
		orderItem.setAsc(false);
		page.addOrder(orderItem);

		QueryWrapper queryWrapper=Wrappers.query();
		if(null!=wmWkAlbum.getXcmc())
		{
			queryWrapper.like(true,"xcmc",wmWkAlbum.getXcmc());
		}

		if(null!=wmWkAlbum.getGlyp())
		{
			queryWrapper.apply(true,"FIND_IN_SET ("+wmWkAlbum.getGlyp()+",glyp)");
		}

		queryWrapper.orderByDesc("create_time");

    	Page pageAnswer =wmWkAlbumService.page(page, queryWrapper);
    	List<WmWkAlbum> wmWkAlbumList=pageAnswer.getRecords();
    	if(null!=wmWkAlbumList)
		{
			for (WmWkAlbum wmW :wmWkAlbumList) {
				if(null!=wmW.getGlyp())
				{
					WmDaDrugEntDrugtable wmDaDrugEntDrugtable=new WmDaDrugEntDrugtable();
//					if(wmW.getGlyp().contains(","))
//					{
						String [] ar=wmW.getGlyp().split(",");
						List<String> list=new ArrayList<>(Arrays.asList(ar));
						List<WmDaDrugEntDrugtable>  wmDaDrugEntDrugtableList=wmDaDrugEntDrugtableService.listByIds(list);
						if(wmDaDrugEntDrugtableList.size()>0)
						{
							String temp1="";
							for (int i=0;i<wmDaDrugEntDrugtableList.size();i++) {
								temp1+=wmDaDrugEntDrugtableList.get(i).getDrugname();
								if(i<wmDaDrugEntDrugtableList.size()-1)
								{
									temp1+=",";
								}
							}
							wmW.setTemp1(temp1);
						}

//					}
				}
			}
		}
		pageAnswer.setRecords(wmWkAlbumList);
        return R.ok(pageAnswer);
    }


    /**
     * 通过id查询文库-相册
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_wmwkalbum_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(wmWkAlbumService.getById(id));
    }

    /**
     * 新增文库-相册
     * @param wmWkAlbum 文库-相册
     * @return R
     */
    @ApiOperation(value = "新增文库-相册", notes = "新增文库-相册")
    @SysLog("新增文库-相册" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_wmwkalbum_add')" )
    public R save(@RequestBody WmWkAlbum wmWkAlbum) {
		wmWkAlbum.setForwardNum(0);
		wmWkAlbum.setCreateTime(LocalDateTime.now());
//        return R.ok(wmWkAlbumService.save(wmWkAlbum));
		wmWkAlbumService.saveWmWkAlbum(wmWkAlbum);
        return R.ok();
    }

    /**
     * 修改文库-相册
     * @param wmWkAlbum 文库-相册
     * @return R
     */
    @ApiOperation(value = "修改文库-相册", notes = "修改文库-相册")
    @SysLog("修改文库-相册" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_wmwkalbum_edit')" )
    public R updateById(@RequestBody WmWkAlbum wmWkAlbum) {
		wmWkAlbumService.updateWmWkAlbumById(wmWkAlbum);
        return R.ok();
    }

    /**
     * 通过id删除文库-相册
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除文库-相册", notes = "通过id删除文库-相册")
    @SysLog("通过id删除文库-相册" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('admin_wmwkalbum_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(wmWkAlbumService.removeById(id));
    }

}
