package cn.pluss.platform.controller.fans;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.Fans;
import cn.pluss.platform.fans.FansService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.FansConsumVO;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("fans")
public class FansController extends BaseNoModelController {

    private final static Integer PAGE_SIZE = 12;

    @Autowired
    private FansService fansService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "fans/fansList";
    }

    /**
     * 分页查询
     *
     * @throws Exception
     */
    @RequestMapping(value = "/queryFansByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryFansByPage(@RequestParam(defaultValue = "1") Integer currPage, String startTime, String endTime, FansConsumVO fansConsumVO) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> result = new HashMap<>(16);
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        Boolean bol = verifyUserRole();
        if (StringUtil.isNotEmpty(fansConsumVO.getNickName())) {
            String nickName = URLEncoder.encode(fansConsumVO.getNickName(), "UTF-8");
            map.put("nickName", nickName);
        }
        try {
            Date startTimeDate = simpleDateFormat.parse(startTime);
            Date endTimeDate = simpleDateFormat.parse(endTime);
            map.put("startTime", startTimeDate);
            map.put("endTime", endTimeDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (StringUtil.isNotEmpty(fansConsumVO.getMerchantName())) {
            map.put("merchantName", fansConsumVO.getMerchantName());
        }
        if (StringUtil.isNotEmpty(fansConsumVO.getLastMerchantName())) {
            map.put("lastMerchantName", fansConsumVO.getLastMerchantName());
        }
        if (StringUtil.isNotEmpty(fansConsumVO.getOrderNumber())) {
            map.put("orderNumber", fansConsumVO.getOrderNumber());
        }
        if (StringUtil.isNotEmpty(fansConsumVO.getLastOrderNumber())) {
            map.put("lastOrderNumber", fansConsumVO.getLastOrderNumber());
        }
        List<FansConsumVO> fansList = null;
        Integer count = 0;
        if (!bol) {
            ShiroUser shiroUser = queryCurrentShiroUser();
            if (StringUtil.isNotEmpty(shiroUser.getMerchantCode())) {
                map.put("merchantCode", shiroUser.getMerchantCode());
            } else {
                map.put("merchantCode", StringUtil.genRandomNum(33));
            }
            fansList = fansService.queryFansNoConsumPage(map);
            count = fansService.queryFansNoConsumPageCount(map);
        }else {
            fansList = fansService.queryFansConsumPage(map);
            count = fansService.queryFansConsumPageCount(map);
            result.put("admin",bol);
        }
        for (FansConsumVO f : fansList) {
            if (StringUtil.isNotEmpty(f.getNickName())) {
                f.setNickName(URLDecoder.decode(f.getNickName(), "UTF-8"));
                f.setNickName(URLDecoder.decode(f.getNickName(), "UTF-8"));
                f.setNickName(URLDecoder.decode(f.getNickName(), "UTF-8"));
                f.setNickName(URLDecoder.decode(f.getNickName(), "UTF-8"));
                f.setNickName(URLDecoder.decode(f.getNickName(), "UTF-8"));
            }
        }
        result.put("fansList", fansList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryFansDetails", method = RequestMethod.GET)
    public String queryFansDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            Fans fans = new Fans();
            fans.setId(id);
            fans = fansService.queryFans(fans);
            request.setAttribute("fans", fans);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "fans/fansDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryFansBaseInfo", method = RequestMethod.GET)
    public String queryFansBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            Fans fans = new Fans();
            fans.setId(id);
            fans = fansService.queryFans(fans);
            request.setAttribute("fans", fans);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "fans/fansBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryFansExtendsInfo", method = RequestMethod.GET)
    public String queryFansExtendsInfo() {

        return "fans/fansExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryFansFunctionsInfo", method = RequestMethod.GET)
    public String queryFansFunctionsInfo() {

        return "fans/fansFunctionsInfo";
    }

    /**
     * 保存对象
     *
     */
    @RequestMapping(value = "/saveFans", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveFans(Fans fans) {

        if (fans.getId() != null && fans.getId() != 0) {
            fansService.updateFans(fans);
        } else {
            fansService.saveFans(fans);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     *
     */
    @RequestMapping(value = "/updateFans", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateFans(Integer id) {

        Fans fans = new Fans();
        fans.setId(id);
        fansService.updateFans(fans);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     */
    @RequestMapping(value = "/deleteFans", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteFans(Integer id) {
        Fans fans = new Fans();
        fans.setId(id);
        fansService.deleteFans(fans);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}


