package com.kmall.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.entity.ThirdMerchantBizEntity;
import com.kmall.admin.service.StoreService;
import com.kmall.admin.service.ThirdMerchantBizService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.admin.utils.ShiroUtils;
import com.kmall.common.constant.Dict;
import com.kmall.admin.fromcomm.entity.SysUserEntity;
import com.kmall.common.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 第三方商户表Controller
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-02-15 11:43:32
 */
@Controller
@RequestMapping("thirdmerchantbiz")
public class ThirdMerchantBizController {
    @Autowired
    private ThirdMerchantBizService thirdMerchantBizService;
    @Autowired
    private StoreService storeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("thirdmerchantbiz:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
        ParamUtils.setName(params, "name");
        //查询列表数据
        Query query = new Query(params);

        List<ThirdMerchantBizEntity> thirdMerchantBizList = thirdMerchantBizService.queryList(query);
        int total = thirdMerchantBizService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(thirdMerchantBizList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{thirdMerchSn}")
    @RequiresPermissions("thirdmerchantbiz:info")
    @ResponseBody
    public R info(@PathVariable("thirdMerchSn") Integer thirdMerchSn) {
        ThirdMerchantBizEntity thirdMerchantBiz = thirdMerchantBizService.queryObject(thirdMerchSn);

        return R.ok().put("thirdMerchantBiz", thirdMerchantBiz);
    }
    /**
     * 查看信息
     */
    @RequestMapping("/infoByCode")
    @ResponseBody
    public R infoByCode(@RequestParam Map<String, Object> params) {
        String thirdMerchantCode = (String) params.get("thirdMerchantCode");
        ThirdMerchantBizEntity thirdMerchantBiz = thirdMerchantBizService.getThirdMerchangByCode(thirdMerchantCode);

        return R.ok().put("thirdMerchantBiz", thirdMerchantBiz);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("thirdmerchantbiz:save")
    @ResponseBody
    public R save(@RequestBody ThirdMerchantBizEntity thirdMerchantBiz) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if(user != null) {
            thirdMerchantBiz.setCreaterSn(user.getUserId().toString());
            thirdMerchantBiz.setModerSn(user.getUserId().toString());
        }else {
            throw new RRException("登陆用户失效，请重新登陆！");
        }
        Map<String, Object> valideDate = MapBeanUtil.fromObject(thirdMerchantBiz);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户编号");
        builder.put("thirdPartyMerchCode", "第三方商户代码");
        builder.put("thirdPartyMerchName", "第三方商户名称");
        builder.put("isStockShare", "库存是否共享");
//        builder.put("isStoreUserShare", "门店用户是否共享");
        builder.put("isMapShow", "商户地图是否显示");
        builder.put("isSupplierGoods", "供应商模式商品是否启用");
        builder.put("isFaceCheck", "是否使用人脸核验");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        thirdMerchantBizService.save(thirdMerchantBiz);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("thirdmerchantbiz:update")
    @ResponseBody
    public R update(@RequestBody ThirdMerchantBizEntity thirdMerchantBiz) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if(user != null) {
            thirdMerchantBiz.setCreaterSn(user.getUserId().toString());
            thirdMerchantBiz.setModerSn(user.getUserId().toString());
        }else {
            throw new RRException("登陆用户失效，请重新登陆！");
        }
        Map<String, Object> valideDate = MapBeanUtil.fromObject(thirdMerchantBiz);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户编号");
        builder.put("thirdPartyMerchCode", "第三方商户代码");
        builder.put("thirdPartyMerchName", "第三方商户名称");
//        builder.put("isStockShare", "库存是否共享");
//        builder.put("isStoreUserShare", "门店用户是否共享");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        thirdMerchantBizService.update(thirdMerchantBiz);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("thirdmerchantbiz:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]thirdMerchSns) {
        thirdMerchantBizService.deleteBatch(thirdMerchSns);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeKey", "merchSn", "thirdPartyMerchCode");
        List<ThirdMerchantBizEntity> list = thirdMerchantBizService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAlls")
    @ResponseBody
    public R queryAlls(@RequestParam Map<String, Object> params) {
        List<ThirdMerchantBizEntity> list = thirdMerchantBizService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/getIsSupplierShowByThirdSn")
    @ResponseBody
    public R getIsSupplierShowByThirdSn(@RequestParam Map<String, Object> params) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if (user == null) {
            throw new RRException("用户登录超时，请重新登录");
        }
        Boolean isSupplierShow = false;
        Boolean roleType = false;
        if (Dict.roleType.item_2.getItem().equals(user.getRoleType())) {
            ThirdMerchantBizEntity thirdMerchantBizEntity  = thirdMerchantBizService.getThirdMerchangByCode(user.getThirdPartyMerchCode());
            if(thirdMerchantBizEntity != null){
                String isSupplierGoods = thirdMerchantBizEntity.getIsSupplierGoods();
                if(isSupplierGoods.equalsIgnoreCase(Dict.isSupplierGoods.item_1.getItem())){
                    isSupplierShow = true;
                }
            }
        }
        if (Dict.roleType.item_3.getItem().equalsIgnoreCase(user.getRoleType()) || Dict.roleType.item_1.getItem().equalsIgnoreCase(user.getRoleType())) {
            isSupplierShow = true;
            roleType = true;
        }
        if (Dict.roleType.item_4.getItem().equals(user.getRoleType())) {
            ThirdMerchantBizEntity thirdMerchantBizEntity  = thirdMerchantBizService.getThirdMerchangByCode(user.getThirdPartyMerchCode());
            if(thirdMerchantBizEntity != null){
                String isSupplierGoods = thirdMerchantBizEntity.getIsSupplierGoods();
                if(isSupplierGoods.equalsIgnoreCase(Dict.isSupplierGoods.item_1.getItem())){
                    isSupplierShow = true;
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isSupplierShow", isSupplierShow);
        map.put("roleType", roleType);
        return R.ok().put("map", map);
    }
}
