package cn.pluss.platform.controller.merchantDevice;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantDevice;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantDevice.MerchantDeviceService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.DeviceVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("merchantDevice")
public class MerchantDeviceController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MerchantDeviceService merchantDeviceService;

    @Autowired
    private MerchantBaseInfoService merchantBaseInfoService;

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

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryMerchantDeviceByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMerchantDeviceByPage(@RequestParam(defaultValue = "1") Integer currPage, MerchantDevice merchantDevice) {
        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);
        if (StringUtil.isNotEmpty(merchantDevice.getName())) {
            map.put("name", merchantDevice.getName());
        }
        Boolean bol = verifyUserRole();
        if (bol) {
            result.put("admin", "admin");
        } else {
            ShiroUser shiroUser = queryCurrentShiroUser();
            if (StringUtil.isNotEmpty(shiroUser.getUserType())) {
                result.put("userType", shiroUser.getUserType());
            }
            map.put("merchantCode", shiroUser.getMerchantCode());
        }
        if (StringUtil.isNotEmpty(merchantDevice.getMerchantName())) {
            map.put("merchantName", merchantDevice.getMerchantName());
        }
        if (StringUtil.isNotEmpty(merchantDevice.getAlias())) {
            map.put("alias", merchantDevice.getAlias());
        }
        if (StringUtil.isNotEmpty(merchantDevice.getUserId())) {
            map.put("userId", merchantDevice.getUserId());
        }
        List<DeviceVO> merchantDeviceList = merchantDeviceService.showMerchantDevicePage(map);
        Integer count = merchantDeviceService.showMerchantDevicePageCount(map);
        result.put("merchantDeviceList", merchantDeviceList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

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

        if (id != null && id != 0) {
            MerchantDevice merchantDevice = new MerchantDevice();
            merchantDevice.setId(id);
            merchantDevice = merchantDeviceService.getOne(new QueryWrapper<>(merchantDevice));
            request.setAttribute("merchantDevice", merchantDevice);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "merchantDevice/merchantDeviceDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantDeviceBaseInfo", method = RequestMethod.GET)
    public String queryMerchantDeviceBaseInfo(HttpServletRequest request, Long id, Integer pageIndex) {
        if (id != null && id != 0) {
            MerchantDevice merchantDevice = new MerchantDevice();
            merchantDevice.setId(id);
            merchantDevice = merchantDeviceService.getOne(new QueryWrapper<>(merchantDevice));
            request.setAttribute("merchantDevice", merchantDevice);
            request.setAttribute("pageIndex", pageIndex);
        }
        getRole();
        getPermission();
        getMerchant();
        return "merchantDevice/merchantDeviceBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantDeviceExtendsInfo", method = RequestMethod.GET)
    public String queryMerchantDeviceExtendsInfo() {
        return "merchantDevice/merchantDeviceExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantDeviceFunctionsInfo", method = RequestMethod.GET)
    public String queryMerchantDeviceFunctionsInfo() {
        return "merchantDevice/merchantDeviceFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveMerchantDevice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveMerchantDevice(MerchantDevice merchantDevice) {

        if (merchantDevice.getId() != null && merchantDevice.getId() != 0) {
            merchantDeviceService.updateById(merchantDevice);
        } else {
            Boolean bol = verifyUserRole();
            if (!bol) {
                ShiroUser shiroUser = queryCurrentShiroUser();
                if (StringUtil.isNotEmpty(shiroUser)) {
                    merchantDevice.setMerchantCode(shiroUser.getMerchantCode());
                }
            }
            merchantDeviceService.save(merchantDevice);
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

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

        MerchantDevice merchantDevice = new MerchantDevice();
        merchantDevice.setId(id);
        merchantDeviceService.updateById(merchantDevice);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteMerchantDevice", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMerchantDevice(Long id) {
        MerchantDevice merchantDevice = new MerchantDevice();
        merchantDevice.setId(id);
        merchantDeviceService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 管理员权限标识
     */
    public void getRole() {
        request.setAttribute("admin", "admin");
    }

    /**
     * 不是管理员 获取的权限
     */
    public void getPermission() {
        ShiroUser user = queryCurrentShiroUser();
        request.setAttribute("userType", user.getUserType());
    }

    /**
     * 获取所有商户信息
     */
    public void getMerchant() {
        List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(null);
        request.setAttribute("merchantBaseInfoList", merchantBaseInfoList);
    }

}


