package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicDataRequirementState;
import com.lightinit.hsdataplatform.dictionary.DicPlatformType;
import com.lightinit.hsdataplatform.dictionary.DicRequirementState;
import com.lightinit.hsdataplatform.dictionary.DicResourceAuditResult;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.DataRequirementDataMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.DataRequirementAddModel;
import com.lightinit.hsdataplatform.model.admin.DataSupplyListModel;
import com.lightinit.hsdataplatform.model.admin.DataSupplySearchModel;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.service.admin.IDataSupplyService;
import org.apache.http.protocol.HTTP;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/25 0025.
 */
@Controller
@RequestMapping("/admin/data_supply")
public class DataSupplyController extends BaseController {
    @Autowired
    private IDataSupplyService dataSupplyService;
    @Autowired
    private DataRequirementDataMapper dataRequirementDataMapper;

    @RequestMapping(value = "index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/index");
        modelAndView.addObject("dicRequirementState", DicDataRequirementState.getInstance().getDicMap());
        modelAndView.addObject("dicPlatformType", DicPlatformType.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(DataSupplySearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo,HttpSession session) throws IOException {
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setUserId(loginUser.getId());
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        if (inputModel.getRequirementState() != null) {
            inputModel.setRequirementState(inputModel.getRequirementState().equals("全部") ? "" : inputModel.getRequirementState());
        }
        if (inputModel.getRequirementSource() != null) {
            inputModel.setRequirementSource(inputModel.getRequirementSource().equals("全部") ? "" : inputModel.getRequirementSource());
        }
        List<DataSupplyListModel> outputModel = dataSupplyService.queryList(inputModel, pager);
        pager.setTotal(dataSupplyService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    @RequestMapping(value = "popAudit.action")
    public ModelAndView popAudit(@RequestParam(defaultValue = "0") int id, HttpSession session) throws IOException {
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/audit");
        DataSupplySearchModel inputModel = new DataSupplySearchModel();
        inputModel.setId(id);
        List<DataSupplyListModel> outputModel = dataSupplyService.queryList(inputModel, null);
        //添加审核人，日期
        modelAndView.addObject("auditAt", new Date());
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        modelAndView.addObject("auditBy",loginUser.getUsername());

        modelAndView.addObject("model", outputModel.get(0));

        DataRequirementDataExample example = new DataRequirementDataExample();
        DataRequirementDataExample.Criteria criteria = example.createCriteria();
        criteria.andRqrmntIdEqualTo((long) id);
        List<DataRequirementData> dataRequirementDatas = dataRequirementDataMapper.selectByExample(example);
        modelAndView.addObject("dataList", dataRequirementDatas);
        return modelAndView;
    }

    @RequestMapping(value = "popFeedback.action")
    public ModelAndView popFeedback(@RequestParam(defaultValue = "0") int id,HttpSession session) throws IOException {
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/feedback");
        DataSupplySearchModel inputModel = new DataSupplySearchModel();
        inputModel.setId(id);
        List<DataSupplyListModel> outputModel = dataSupplyService.queryList(inputModel, null);
        modelAndView.addObject("model", outputModel.get(0));
        //反馈日期，人
        modelAndView.addObject("feedbackAt",new Date());
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        modelAndView.addObject("feedbackBy",loginUser.getUsername());
        DataRequirementDataExample example = new DataRequirementDataExample();
        DataRequirementDataExample.Criteria criteria = example.createCriteria();
        criteria.andRqrmntIdEqualTo((long) id);
        List<DataRequirementData> dataRequirementDatas = dataRequirementDataMapper.selectByExample(example);
        modelAndView.addObject("dataList", dataRequirementDatas);

        return modelAndView;
    }

    @RequestMapping(value = "popDetail.action")
    public ModelAndView popDetail(@RequestParam(defaultValue = "0") int id) throws IOException {
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/detail");
        DataSupplySearchModel inputModel = new DataSupplySearchModel();
        inputModel.setId(id);
        List<DataSupplyListModel> outputModel = dataSupplyService.queryList(inputModel, null);
        modelAndView.addObject("model", outputModel.get(0));

        DataRequirementDataExample example = new DataRequirementDataExample();
        DataRequirementDataExample.Criteria criteria = example.createCriteria();
        criteria.andRqrmntIdEqualTo((long) id);
        List<DataRequirementData> dataRequirementDatas = dataRequirementDataMapper.selectByExample(example);
        modelAndView.addObject("dataList", dataRequirementDatas);

        return modelAndView;
    }

    @RequestMapping(value = "audit.action")
    @ResponseBody
    public ResultState doAudit(@RequestParam(defaultValue = "0") int id,
                               @RequestParam(defaultValue = "") String auditType,
                               @RequestParam(defaultValue = "") String auditOpinion, HttpSession session) {
        ResultState resultState = new ResultState();
        if (StringUtils.isEmpty(auditType) || id == 0) {
            resultState.setMsg("参数错误");
            return resultState;
        }
        Requirement requirement = new Requirement();
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        requirement.setAuditedBy(loginUser.getId());
        requirement.setId((long) id);
        requirement.setAuditResult(auditType);
        requirement.setAuditOpinion(auditOpinion);
        resultState = dataSupplyService.SetAuditOpinion(requirement);
        return resultState;
    }

    @RequestMapping(value = "feedback.action")
    @ResponseBody
    public ResultState doFeedbcak(@RequestParam(defaultValue = "0") int id, @RequestParam(defaultValue = "") String feedbackResult,
                                  @RequestParam(defaultValue = "") String feedbackDesc,HttpSession session) {
        ResultState resultState = new ResultState();
        if (StringUtils.isEmpty(feedbackResult) || id == 0) {
            resultState.setMsg("参数错误");
            return resultState;
        }
        RequirementData requirementData = new RequirementData();
        requirementData.setId((long) id);
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        requirementData.setFeedbackBy(loginUser.getId());
        requirementData.setFeedbackResult(feedbackResult);
        requirementData.setFeedbackDesc(feedbackDesc);
        requirementData.setFeedbackAt(new Date());
        resultState = dataSupplyService.SetFeedbackDesc(requirementData);
        return resultState;
    }


//    新增发布数据需求
    @RequestMapping(value = "publishRequirement.htm")
    public ModelAndView publishRequirementIndex(){
        ModelAndView modelAndView = new ModelAndView("admin/data_supply/publishRequirementIndex");
        List<Map<Long,String>> list=dataSupplyService.getSectorParentNames();
        modelAndView.addObject("sectorNames",list);
        return modelAndView;
    }

    @RequestMapping(value = "publishRequirement.action")
    @ResponseBody
    public ResultState publishRequirement(@Valid DataRequirementAddModel dataRequirementAddModel, BindingResult bindingResult, HttpSession session){
        if(bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(dataRequirementAddModel, bindingResult);
        }
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        dataRequirementAddModel.setUserId(loginUser.getId());
        dataRequirementAddModel.setPlatformType(DicPlatformType.OPEN);
        ResultState resultState= dataSupplyService.addDataRequirement(dataRequirementAddModel);
        return resultState;
    }
}
