package com.ruoyi.web.controller.Ytjback;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.GtgsZx;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IGtgsZxService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.tool.FtlToWordUtils;
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.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 注销前台Controller
 *
 * @author ruoyi
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/system/ytj/zx")
public class GtgsZxBackController extends BaseController {
    private String prefix = "system/gtgszx";

    @Autowired
    private IGtgsZxService gtgsZxService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("ytj:zx:view")
    @GetMapping("/{flag}")
    public String zx(@PathVariable("flag") String flag, ModelMap modelMap) {
        modelMap.addAttribute("flag", flag);
        return prefix + "/zx";
    }

    /**
     * 查询注销前台列表
     */
    @RequiresPermissions("ytj:zx:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GtgsZx gtgsZx) {
        if (String.valueOf(1).equals(gtgsZx.getFlag()) && !ShiroUtils.getLoginName().equals("admin")) {
            gtgsZx.setArchiveBy(ShiroUtils.getSysUser().getUserName());
        }
        startPage();
        List<GtgsZx> list = gtgsZxService.selectGtgsZxList(gtgsZx);
        return getDataTable(list);
    }

    /**
     * 导出注销前台列表
     */
    @RequiresPermissions("ytj:zx:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GtgsZx gtgsZx) {
        List<GtgsZx> list = gtgsZxService.selectGtgsZxList(gtgsZx);
        ExcelUtil<GtgsZx> util = new ExcelUtil<GtgsZx>(GtgsZx.class);
        return util.exportExcel(list, "zx");
    }

    /**
     * 新增保存注销前台
     */
    @RequiresPermissions("ytj:zx:add")
    @Log(title = "注销前台", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GtgsZx gtgsZx) {
        return toAjax(gtgsZxService.insertGtgsZx(gtgsZx));
    }


    /**
     * 删除注销前台
     */
    @RequiresPermissions("ytj:zx:remove")
    @Log(title = "注销前台", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gtgsZxService.deleteGtgsZxByIds(ids));
    }

    /**
     * 审核注销前台列表
     */
    @RequiresPermissions("ytj:zx:edit")
    @GetMapping("/check/{id}")
    public String checkZX(@PathVariable("id") String id, ModelMap modelMap) {
        Calendar calendar = Calendar.getInstance();
        GtgsZx gtgsZx = gtgsZxService.selectGtgsZxById(id);
        if (gtgsZx.getArchiveBy() == null || gtgsZx.getArchiveBy().isEmpty() || gtgsZx.getArchiveBy().length() == 0 || gtgsZx.getArchiveBy().equals(ShiroUtils.getSysUser().getUserName())||ShiroUtils.getSysUser().isAdmin()) {
            gtgsZx.setCheckTime(DateUtils.getNowDate());
            if (gtgsZx.getArchiveBy() == null || gtgsZx.getArchiveBy().isEmpty() ) {
                gtgsZx.setArchiveBy(ShiroUtils.getSysUser().getUserName());
            }
            gtgsZx.setFlag(String.valueOf(1));
            gtgsZxService.changeGtgsZxFlagTg(gtgsZx);
            modelMap.addAttribute("gtgsZx", gtgsZx);
            modelMap.addAttribute(
                    "year", calendar.get(Calendar.YEAR));
            return prefix + "/check";
        } else {
            throw new BusinessException("服务器出现错误或这个申请已被他人处理，请刷新后重试！");
        }
    }

    /**
     * 审核注销前台列表
     */
    @RequiresPermissions("ytj:zx:edit")
    @GetMapping("/view/{id}")
    public String viewZX(@PathVariable("id") String id, ModelMap modelMap) {
        GtgsZx gtgsZx = gtgsZxService.selectGtgsZxById(id);

        gtgsZx.setArchiveBy(gtgsZx.getArchiveBy());

        modelMap.addAttribute("gtgsZx", gtgsZx);
        return prefix + "/view";
    }

    /**
     * 通过
     */
    @RequiresPermissions("ytj:zx:edit")
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(GtgsZx gtgsZx) {
        GtgsZx gtgsZx1 = gtgsZxService.selectGtgsZxById(gtgsZx.getId());
        if (Integer.valueOf(gtgsZx1.getFlag()) > 1) {
            return AjaxResult.warn("此申请已被处理完成！");
        } else if (!gtgsZx1.getArchiveBy().equals(ShiroUtils.getSysUser().getUserName())) {
            return AjaxResult.warn("此申请已归到他人审核！您没有权限处理。");
        }
        gtgsZx.setArchiveTime(DateUtils.getNowDate());
        return toAjax(gtgsZxService.changeGtgsZxFlagTg(gtgsZx));
    }

    /**
     * 下载档案
     */
    @RequiresPermissions("ytj:zx:edit")
    @PostMapping("/download")
    @ResponseBody
    public AjaxResult download(String id) {
        try {
            GtgsZx gtgsZx = gtgsZxService.selectGtgsZxById(id);
            //签名
            gtgsZx.setOperatorSignature(delPicBase64Head(gtgsZx.getOperatorSignature()));
            //身份证正面
            gtgsZx.setOperatorIdCardFront(delPicBase64Head(gtgsZx.getOperatorIdCardFront()));
            //身份证反面
            gtgsZx.setOperatorIdCardBack(delPicBase64Head(gtgsZx.getOperatorIdCardBack()));
            if (gtgsZx.getBusinessLicenseProve() != null && !gtgsZx.getBusinessLicenseProve().trim().equals("")) {
                gtgsZx.setBusinessLicenseProve(delPicBase64Head(gtgsZx.getBusinessLicenseProve()));
            }
            if (gtgsZx.getClearTaxProve() != null && !gtgsZx.getClearTaxProve().trim().equals("")) {
                gtgsZx.setClearTaxProve(delPicBase64Head(gtgsZx.getClearTaxProve()));
            }
            Map<String, Object> valueMap = MapUtils.getValueMap(gtgsZx);
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
            valueMap.put("checkTimeStr", format.format(gtgsZx.getCheckTime()));
            valueMap.put("archiveTimeStr", format.format(gtgsZx.getArchiveTime()));
            valueMap.put("city", Global.getCityName());
            FtlToWordUtils dh = new FtlToWordUtils();
            String docPath = dh.createDoc("gtgszx.ftl", gtgsZx.getCompanyName(), valueMap);
            return AjaxResult.success(docPath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    public String delPicBase64Head(String str) {
        return str.replace("data:image/jgp;base64,", "").replace("data:image/png;base64,", "").replace("data:image/jpeg;base64,", "");
    }
}
