package com.ruoyi.project.project.cases.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.project.cases.domain.Case;
import com.ruoyi.project.project.cases.service.ICaseService;
import com.ruoyi.project.project.item.domain.Item;
import com.ruoyi.project.project.item.service.IItemService;
import com.ruoyi.project.project.util.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 案件 信息操作处理
 *
 * @author luoluo
 */
@Controller
@RequestMapping("/system/case")
public class CaseController extends BaseController
{
    private String prefix = "project/case";

    @Autowired
    private ICaseService caseService;

    @Autowired
    private IItemService itemService;

    @RequiresPermissions("system:case:view")
    @GetMapping()
    public String cases()
    {
        return prefix + "/case";
    }

    /**
     * 查询案件列表
     */
    @RequiresPermissions("system:case:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Case cases)
    {
        startPage();
        List<Case> list = caseService.selectCaseList(cases);
        return getDataTable(list);
    }

    /**
     * 新增案件
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存案件
     */
    @RequiresPermissions("system:case:add")
    @Log(title = "案件新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Case cases)
    {
        int i = caseService.selectCaseByCaseNo(cases.getCaseNo());
        if(i != 0){
            return error("案件编号重复,请检查！！！！");
        }
        boolean phone = ValidatorUtils.isValidPhone(cases.getPhone());
        if (!phone){
            return error("手机号格式错误请检查,请检查！！！！");
        }
        boolean userNo = ValidatorUtils.isValidIdCard(cases.getUserNo());
        if (!userNo){
            return error("身份证号格式错误请检查,请检查！！！！");
        }
        return toAjax(caseService.insertCase(cases));
    }

    /**
     * 导出案件列表
     */
    @RequiresPermissions("system:case:export")
    @Log(title = "案件导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Case cases)
    {
        List<Case> list = caseService.selectCaseList(cases);
        ExcelUtil<Case> util = new ExcelUtil<Case>(Case.class);
        return util.exportExcel(list, "案件数据");
    }



    /**
     * 修改案件信息
     */
    @RequiresPermissions("system:case:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        mmap.put("case", caseService.selectCaseById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存案件
     */
    @RequiresPermissions("system:case:edit")
    @Log(title = "案件修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Case cases)
    {
        boolean phone = ValidatorUtils.isValidPhone(cases.getPhone());
        if (!phone){
            return error("手机号格式错误请检查,请检查！！！！");
        }
        boolean userNo = ValidatorUtils.isValidIdCard(cases.getUserNo());
        if (!userNo){
            return error("身份证号格式错误请检查,请检查！！！！");
        }
        return toAjax(caseService.updateCase(cases));
    }

    /**
     * 删除案件
     */
    @RequiresPermissions("system:case:remove")
    @Log(title = "删除案件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //查询是否有物品信息
        String[] idStr = ids.split(",");
        for (String id : idStr) {
            Case acase = caseService.selectCaseById(Integer.parseInt(id));
            int i = itemService.selectItemByCaseId(id);
            if (i != 0){
                return error(acase.getCaseNo()+"案件中存在物品，请先删除物品！！！");
            }
        }

        return toAjax(caseService.deleteCaseByIds(ids));
    }

    /**
     * 添加案件物品条目
     */
    @RequiresPermissions("system:case:list")
    @GetMapping("/detail/{caseNo}")
    public String detail(@PathVariable("caseNo") String caseNo, ModelMap mmap)
    {
        mmap.put("caseNo",caseNo);
        return "project/item/item";
    }

}


