package com.zcloud.controller.taxationManage;

import com.alibaba.excel.util.DateUtils;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.assignedManage.AssignedManageService;
import com.zcloud.service.freighttrailer.FreightTrailerService;
import com.zcloud.service.system.DictionariesService;
import com.zcloud.service.taxationManage.TaxationManageService;
import com.zcloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Controller
@RequestMapping("/taxationmanage")
public class TaxationManageController extends BaseController {

    @Autowired
    private TaxationManageService taxationManageService;

    @Autowired
    private Smb smb;

    @Autowired
    private AssignedManageService assignedManageService;

    @Autowired
    private FreightTrailerService freightTrailerService;

    /**新增
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/add")
    @ResponseBody
    public Object add() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("TAXATION_ID", this.get32UUID());	//车船税主键
        pd.put("ISDELETE", "0");	//是否删除(0:有效 1:删除)
        pd.put("CREATOR", Jurisdiction.getUSER_ID());	//添加人id
        pd.put("CREATORNAME", Jurisdiction.getUsername());	//添加人名字
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
        pd.put("ARCHIVES_TYPE", "0");    //档案状态
        if("".equals(pd.getString("EXPIRE_STATUS"))) {
            String expireDate = pd.getString("EXPIRE_DATE");
            String day = DateUtil.getDay();
            boolean flag = DateUtil.compareDate(expireDate, day);
            if(flag) {
                pd.put("EXPIRE_STATUS","1");
            }else{
                pd.put("EXPIRE_STATUS","0");
            }
        }
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        taxationManageService.save(pd);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**删除
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object delete() throws Exception{
        Map<String,String> map = new HashMap<String,String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("DELETOR", Jurisdiction.getUSER_ID());
        pd.put("DELETORNAME", Jurisdiction.getUsername());
        pd.put("DELETTIME", DateUtil.date2Str(new Date()));
        taxationManageService.delete(pd);
        map.put("result", errInfo);				//返回结果
        return map;
    }

    /**修改
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Object edit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("ISDELETE", "0");	//是否删除(0:有效 1:删除)
        pd.put("CREATOR", Jurisdiction.getUSER_ID());	//添加人id
        pd.put("CREATORNAME", Jurisdiction.getUsername());	//添加人名字
        pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
        pd.put("OPERATOR", Jurisdiction.getUSER_ID());	//修改人id
        pd.put("OPERATORNAME", Jurisdiction.getUsername()); // 修改人名字
        pd.put("OPERATTIME", DateUtil.date2Str(new Date())); // 修改时间
        String expireDate = pd.getString("EXPIRE_DATE");
        String day = DateUtil.getDay();
        boolean flag = DateUtil.compareDate(expireDate, day);
        if(flag) {
            pd.put("EXPIRE_STATUS","1");
        }else{
            pd.put("EXPIRE_STATUS","0");
        }
        taxationManageService.edit(pd);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }
    /**去修改页面获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = taxationManageService.findById(pd);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value="/list")
    @ResponseBody
    public Object list(Page page) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
        if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(pd);
        List<PageData> taxationList = taxationManageService.list(page);	//列出MajorDangerSource列表
        List<PageData> assignedList = assignedManageService.assignedList(Jurisdiction.getCORPINFO_ID());
        List<PageData> varList = new ArrayList<>();
        for (int i = 0; i < taxationList.size(); i++) {
            for (int j = 0; j < assignedList.size(); j++) {
                if(taxationList.get(i).get("PLATE_NUMBER").equals(assignedList.get(j).get("RAW_PLATE_NUMBER"))) {
                    taxationList.get(i).put("RECORD_STATUS","过户");
                    taxationList.get(i).put("ASSIGNEDSTATUS","1");
                }
            }
            varList.add(taxationList.get(i));
        }
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value = "/taxationList")
    @ResponseBody
    public Object taxationList() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        String corpId = Jurisdiction.getCORPINFO_ID();
        List<PageData> list = taxationManageService.taxationList(corpId);    //列出Question列表
        map.put("list", list);
        map.put("result", errInfo);
        return map;
    }

    @RequestMapping(value = "/choicetrailerlist")
    @ResponseBody
    public Object choiceTrailerList(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());    //企业ID
        page.setPd(pd);
        List<PageData> varList = freightTrailerService.list(page);    // 列出所有挂车列表
        List<PageData> taxationList = taxationManageService.taxationList(pd.getString("CORPINFO_ID"));   // 列出车船税列表
        for (int i = 0; i < taxationList.size(); i++) {
            for (int j = 0; j < varList.size(); j++) {
                if (taxationList.get(i).get("PLATE_NUMBER").equals(varList.get(j).get("PLATE_NUMBER")) && varList.get(j).get("ISSCRAP").equals("1")) {
                    varList.remove(j);
                }
            }
        }
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }
}