package com.sifang.modules.supply.stock.controller;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.TraderConstants;
import com.sifang.modules.customer.constant.UserConstants;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.supply.setting.service.CrmSupplyWarehouseSettingService;
import com.sifang.modules.supply.stock.service.SupplyMaterialKeepService;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "${adminPath}/materialKeep")
public class SupplyMaterialKeepController extends BaseController {


    @Autowired
    private SupplyMaterialKeepService keepService;
    @Autowired
    CrmSupplyWarehouseSettingService crmSupplyWarehouseSettingService;
    @Autowired
    TraderService traderService;

    @Autowired
    AddressService addressService;

    @RequestMapping(value = "inOrOutRecord")
    public String inOrOutRecord(Model model){
        //获取单位/物料类型/物料组/仓库
        User user= UserUtils.getUser();
        MaterialUtils.returnModelMaterial(model,"",user.getTraderId());
        model.addAttribute("deliveryMethods",MaterialUtils.getDeliveryMethod());
        List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
        model.addAttribute("traderList", traderList);
        List<Record> supplyList = crmSupplyWarehouseSettingService.getSupplyList();
        model.addAttribute("supplyList",supplyList);
        model.addAttribute("userType", CrmUtils.getCurrentUserType(user));
        if(UserConstants.TYPE_TRADER.equals(user.getUserType()) || UserConstants.TYPE_PLAT_EMP.equals(user.getUserType())){
           //代理商
            List<Record> warehouseList;
            if (Objects.equals(TraderConstants.TYPE_TWO, user.getTraderType())) {
                warehouseList = MaterialUtils.findWarehouseBySecondTrader(user.getTraderId());
                model.addAttribute("wares", warehouseList);
            }
        }
        return "modules/supply/material/materialKeep";
    }

    @ResponseBody
    @RequestMapping(value="materialKeepList")
    public Object materialKeepList(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        keepService.materialKeepList(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "outWarehouseRecord")
    public String outWarehouseRecord(Model model){
        User user= UserUtils.getUser();
        MaterialUtils.returnModelMaterial(model,"",user.getTraderId());
        model.addAttribute("deliveryMethods",MaterialUtils.getDeliveryMethod());
        List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
        model.addAttribute("traderList", traderList);
        List<Record> supplyList = crmSupplyWarehouseSettingService.getSupplyList();
        model.addAttribute("supplyList",supplyList);
        model.addAttribute("userType", CrmUtils.getCurrentUserType(user));
        if(UserConstants.TYPE_TRADER.equals(user.getUserType()) || UserConstants.TYPE_PLAT_EMP.equals(user.getUserType())){
            //代理商
            List<Record> warehouseList;
            if (Objects.equals(TraderConstants.TYPE_TWO, user.getTraderType())) {
                warehouseList = MaterialUtils.findWarehouseBySecondTrader(user.getTraderId());
                model.addAttribute("wares", warehouseList);
            }
        }
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("province", pros);
        return "modules/supply/material/outWarehouseMaterialKeepList";
    }


    //进销存统计页面
    @RequestMapping(value = "fittingInventoryStatistics")
    public String fittingInventoryStatistics(Model model){
        User user = UserUtils.getUser();
        MaterialUtils.returnModelMaterial(model,"",user.getTopTraderId());
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String startTime=dateFormat.format(calendar.getTime());
        model.addAttribute("startTime",startTime);
        return "modules/supply/material/fittingInventoryStatistics";
    }

    @ResponseBody
    @RequestMapping(value = "fittingInventoryStatisticsData")
    public Object fittingInventoryStatisticsData(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        keepService.fittingInventoryStatisticsData(page,map);
        return page.renderJson();
    }

    /**
     *退回例外统计
     */
    @RequestMapping(value = "exceptionOrderStatistics")
    public String exceptionOrderStatistics(Model model){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String startTime=dateFormat.format(calendar.getTime());
        model.addAttribute("startTime",startTime);
        return "modules/supply/material/exceptionOrderStatistics";
    }

    @ResponseBody
    @RequestMapping(value = "exceptionOrderStatisticsData")
    public Object exceptionOrderStatisticsData(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        keepService.exceptionOrderStatisticsData(page,map);
        return page.renderJson();
    }




    @RequestMapping(value = "materTurnoverRate")
    public String materTurnoverRate(Model model){
        //获取单位/物料类型/物料组/仓库
        User user = UserUtils.getUser();
       MaterialUtils.returnModelMaterial(model,"",user.getTopTraderId());
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        String startTime=dateFormat.format(calendar.getTime());
       /* List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
        model.addAttribute("traderList", traderList);
        List<Record> supplyList = crmSupplyWarehouseSettingService.getSupplyList();
        model.addAttribute("supplyList",supplyList);*/
        model.addAttribute("startTime",startTime);
        return "modules/supply/material/materialKeepTurnoverRate";
    }

    /**
     * 获取物料归属
    */
    @ResponseBody
    @RequestMapping(value="findMaterialOwnership")
    public Object findMaterialOwnership(HttpServletRequest request){
        String type=request.getParameter("type");
        return JsonKit.toJson(MaterialUtils.MaterialOwnership(type));
    }

    @ResponseBody
    @RequestMapping(value="fittingTurnOverStatistics")
    public Object fittingTurnOverStatistics(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        keepService.fittingTurnOverStatistics(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "exceptionOrderStaticList")
    public String exceptionOrderStaticList(Model model, HttpServletRequest request) {
        Map<String,String> map = getParams(request);
        model.addAttribute("details", keepService.exceptionOrderStaticList(map));
        //判断是供应商还是代理商
        String ascriptionId=request.getParameter("ascriptionId");
        Record supplier = Db.findFirst("select id from crm_supply_supplier where id=? and status='0'", ascriptionId);
        if(supplier!=null){
          model.addAttribute("type","2");   //供应商
        }else{
            model.addAttribute("type","3");   //代理商
        }
        return "modules/supply/material/exceptionOrderStaticList";
    }

    @ResponseBody
    @RequestMapping(value="exportExceptionOrder")
    public void exportExceptionOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> map = getParams(request);
        keepService.exportExceptionOrder(map,response);
    }


}
