package cn.ezhizao.project.business.personSeal.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.ezhizao.common.utils.SecurityUtils;
import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.personSeal.domain.BizPersonalSeal;
import cn.ezhizao.project.business.personSeal.service.IBizPersonalSealService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 电子图纸Controller
 *
 * @author ezhizao
 * @date 2024-07-04
 */
@RestController
@RequestMapping("/business/personalSeal")
public class BizPersonalSealController extends BaseController
{
    @Resource
    private IBizPersonalSealService bizPersonalSealService;

    /**
     * 查询电子图纸列表
     */
    @GetMapping("/list")
    @Log(title = "个人印章查询", businessType = BusinessType.SELECT)
    public TableDataInfo list(BizPersonalSeal bizPersonalSeal) throws NoSuchFieldException, IllegalAccessException
    {
        startPage();
        List<BizPersonalSeal> list = bizPersonalSealService.getList(bizPersonalSeal);
        return getDataTable(list);
    }


    /**
     * 查询电子图纸列表
     */
    @GetMapping("/listByid")
    @Log(title = "查询当前用户的印章", businessType = BusinessType.SELECT)
    public AjaxResult listByid() throws NoSuchFieldException, IllegalAccessException
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<BizPersonalSeal> list = bizPersonalSealService.query().eq("user_id",userId).orderByDesc("create_time").list();
        if (list.isEmpty()){
            return success();
        }else {
            return success(list.get(0));
        }

    }
    /**
     * 导出电子图纸列表
     */
    @Log(title = "个人印章导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPersonalSeal bizPersonalSeal) throws NoSuchFieldException, IllegalAccessException
    {
        List<BizPersonalSeal> list = bizPersonalSealService.getList(bizPersonalSeal);
        ExcelUtil<BizPersonalSeal> util = new ExcelUtil<BizPersonalSeal>(BizPersonalSeal.class);
        util.exportExcel(response, list, "电子图纸数据");
    }

    /**
     * 获取电子图纸详细信息
     */
    @GetMapping(value = "/{id}")
    @Log(title = "个人印章查询详细信息", businessType = BusinessType.SELECT)
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizPersonalSealService.getById(id));
    }

    /**
     * 新增电子图纸
     */
    @Log(title = "个人印章新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizPersonalSeal bizPersonalSeal) throws NoSuchFieldException, IllegalAccessException
    {
        BizPersonalSeal item = new BizPersonalSeal();
        item.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<BizPersonalSeal> list = bizPersonalSealService.getList(item);
        if (list.size() > 0){
            list.get(0).setUrl(bizPersonalSeal.getUrl());
            list.get(0).setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
            list.get(0).setNickName(SecurityUtils.getLoginUser().getUser().getNickName());
           return toAjax( bizPersonalSealService.saveOrUpdate(list.get(0)));
        }else {
        bizPersonalSeal.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        bizPersonalSeal.setNickName(SecurityUtils.getLoginUser().getUser().getNickName());
        return toAjax(bizPersonalSealService.saveOrUpdate(bizPersonalSeal));}
    }

    /**
     * 修改电子图纸
     */
    @Log(title = "电子图纸修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPersonalSeal bizPersonalSeal) throws NoSuchFieldException, IllegalAccessException
    {
        return toAjax(bizPersonalSealService.updateById(bizPersonalSeal));
    }

    /**
     * 删除电子图纸
     */
    @Log(title = "个人印章删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizPersonalSealService.removeBatchByIds(ids));
    }
}
