package com.sooka.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.sooka.common.annotation.Log;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.core.page.TableDataInfo;
import com.sooka.common.enums.BusinessType;
import com.sooka.common.utils.poi.ExcelUtil;
import com.util.HttpUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sooka.system.domain.TUSfz;
import com.sooka.system.service.ITUSfzService;

import javax.servlet.http.HttpServletRequest;


/**
 * 身份证Controller
 *
 * @author lei
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/system/sfz")
public class TUSfzController extends BaseController {
    private String prefix = "system/sfz";

    @Autowired
    private ITUSfzService tUSfzService;

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

    /**
     * 查询身份证列表
     */
    @RequiresPermissions("system:sfz:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TUSfz tUSfz, HttpServletRequest request) {
        startPage();

        String osAndBrowser[] = HttpUtil.getOsAndBrowserInfo(request).split("---");
        String os = osAndBrowser[0];
        String browser = osAndBrowser[1];
        String ipaddress = HttpUtil.getIPAddress(request);
        tUSfz.setBrowser(browser);
        tUSfz.setOs(os);
        tUSfz.setIpaddress(ipaddress);
        List<TUSfz> list = tUSfzService.selectTUSfzList(tUSfz);
        if(list == null){
            return getDataTable("错了");
        }
        return getDataTable(list);
    }



    /**
     * 导出身份证列表
     */
    @RequiresPermissions("system:sfz:export")
    @Log(title = "身份证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TUSfz tUSfz) {
        List<TUSfz> list = tUSfzService.selectTUSfzList(tUSfz);
        ExcelUtil<TUSfz> util = new ExcelUtil<TUSfz>(TUSfz.class);
        return util.exportExcel(list, "sfz");
    }

    /**
     * 新增身份证
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存身份证
     */
    @RequiresPermissions("system:sfz:add")
    @Log(title = "身份证", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TUSfz tUSfz) {
        return toAjax(tUSfzService.insertTUSfz(tUSfz));
    }

    /**
     * 修改身份证
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TUSfz tUSfz = tUSfzService.selectTUSfzById(id);
        mmap.put("tUSfz", tUSfz);
        return prefix + "/edit";
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap){
        TUSfz tuSfz = tUSfzService.selectTUSfzById(id);
        mmap.put("tUSfz",tuSfz);
        mmap.put("photograph",tuSfz.getPhotograph());
        return "/system/sfz/detail";
    }

    /**
     * 修改保存身份证
     */
    @RequiresPermissions("system:sfz:edit")
    @Log(title = "身份证", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TUSfz tUSfz) {
        return toAjax(tUSfzService.updateTUSfz(tUSfz));
    }

    /**
     * 删除身份证
     */
    @RequiresPermissions("system:sfz:remove")
    @Log(title = "身份证", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tUSfzService.deleteTUSfzByIds(ids));
    }
}
