package com.yinet.web.controller.bjtj;

import java.util.Date;
import java.util.List;

import com.yinet.bjtj.domain.BjljStatistics;
import com.yinet.bjtj.service.IBjljStatisticsService;
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 com.yinet.common.annotation.Log;
import com.yinet.common.enums.BusinessType;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.utils.poi.ExcelUtil;
import com.yinet.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author yinet
 * @date 2020-04-23
 */
@Controller
@RequestMapping("/system/statistics")
public class BjljStatisticsController extends BaseController
{
    private String prefix = "system/statistics";

    @Autowired
    private IBjljStatisticsService bjljStatisticsService;

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

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:statistics:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BjljStatistics bjljStatistics)
    {
        startPage();
        List<BjljStatistics> list = bjljStatisticsService.selectBjljStatisticsList(bjljStatistics);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:statistics:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BjljStatistics bjljStatistics)
    {
        List<BjljStatistics> list = bjljStatisticsService.selectBjljStatisticsList(bjljStatistics);
        ExcelUtil<BjljStatistics> util = new ExcelUtil<BjljStatistics>(BjljStatistics.class);
        return util.exportExcel(list, "statistics");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:statistics:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BjljStatistics bjljStatistics)
    {
        System.err.println("添加："+bjljStatistics);
        bjljStatistics.setAddtime(new Date());
        return toAjax(bjljStatisticsService.insertBjljStatistics(bjljStatistics));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        BjljStatistics bjljStatistics = bjljStatisticsService.selectBjljStatisticsById(id);
        mmap.put("bjljStatistics", bjljStatistics);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:statistics:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BjljStatistics bjljStatistics)
    {
        System.err.println("修改："+bjljStatistics);
        bjljStatistics.setEdittime(new Date());
        return toAjax(bjljStatisticsService.updateBjljStatistics(bjljStatistics));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:statistics:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bjljStatisticsService.deleteBjljStatisticsByIds(ids));
    }

    //总计
    @PostMapping("/zj")
    @ResponseBody
    public AjaxResult zj() {
        //总计
        String quantity=bjljStatisticsService.BjljStatisticsTypeSum("");
        String number=bjljStatisticsService.BjljStatisticsNumberSum("");
        int sum=Double.valueOf(quantity).intValue()+Double.valueOf(number).intValue();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("sum",sum);
        return ajax;
    }

    //总计企业和个体
    @PostMapping("/type")
    @ResponseBody
    public AjaxResult type(@RequestParam("type") String type) {
        //总计企业和个体
        System.err.println("type:"+type);
        String s="";
        if ("个体".equals(type)){
            s=bjljStatisticsService.BjljStatisticsTypeSum(type);
        }else if("企业".equals(type)){
            s=bjljStatisticsService.BjljStatisticsNumberSum(type);
        }

        System.err.println("s:"+s);
        int sum=0;
        if(!s.equals("")){
            sum=Double.valueOf(s).intValue();
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("sum",sum);
        return ajax;
    }
    //乡镇总数
    @PostMapping("/areaName")
    @ResponseBody
    public AjaxResult areaName(@RequestParam("areaName") String areaName) {
        //总计乡镇个数
        String s=bjljStatisticsService.BjljStatisticsAreaNameSum(areaName);
        int sum=0;
        if(s!=null){
            sum=Double.valueOf(s).intValue();
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("sum",sum);
        return ajax;
    }
}
