package com.kmall.admin.controller.mk.store;

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

import com.kmall.admin.entity.mk.store.MkStoreTicketGoodsTempEntity;
import com.kmall.admin.service.mk.store.MkStoreTicketGoodsTempService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.*;
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 emato
 * @email admin@qhdswl.com
 * @date 2019-05-09 17:33:29
 */
@Controller
@RequestMapping("mkstoreticketgoodstemp")
public class MkStoreTicketGoodsTempController {
    @Autowired
    private MkStoreTicketGoodsTempService mkStoreTicketGoodsTempService;

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

        List<MkStoreTicketGoodsTempEntity> mkStoreTicketGoodsTempList = mkStoreTicketGoodsTempService.queryList(query);
        int total = mkStoreTicketGoodsTempService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{discGoodTempId}")
    @RequiresPermissions("mkstoreticketgoodstemp:info")
    @ResponseBody
    public R info(@PathVariable("discGoodTempId") Integer discGoodTempId) {
        MkStoreTicketGoodsTempEntity mkStoreTicketGoodsTemp = mkStoreTicketGoodsTempService.queryObject(discGoodTempId);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkstoreticketgoodstemp:save")
    @ResponseBody
    public R save(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketGoodsTemp) {
        mkStoreTicketGoodsTempService.save(mkStoreTicketGoodsTemp);

        return R.ok();
    }

    /**
     * 弹框中确认按钮批量添加
     * @param mkStoreTicketDiscountGoods
     * @return
     */
    @RequestMapping("/batchSaveDiscountGoods")
//    @RequiresPermissions("mkstoreticketgoodstemp:batchSaveDiscountGoods")
    @ResponseBody
    public R batchSaveDiscountGoods(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketDiscountGoods) {
        mkStoreTicketGoodsTempService.batchSaveDiscountGoods(mkStoreTicketDiscountGoods);
        return R.ok();
    }

    /**
     * 取消选中当前优惠券商品，将商品状态修改为无效
     * @param mkStoreTicketDiscountGoods
     * @return
     */
    @RequestMapping("/updateTempByIsValid")
//    @RequiresPermissions("mkstoreticketgoodstemp:updateTempByIsValid")
    @ResponseBody
    public R updateTempByIsValid(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketDiscountGoods) {
        MkStoreTicketGoodsTempEntity entity = mkStoreTicketGoodsTempService.queryGoodsTempByRealId(mkStoreTicketDiscountGoods.getStoreRelaId(), mkStoreTicketDiscountGoods.getGuid());
        if(entity != null){
            MkStoreTicketGoodsTempEntity tempEntity = new MkStoreTicketGoodsTempEntity();
            tempEntity.setIsValid(Dict.isValid.item_1.getItem());
            tempEntity.setDiscGoodTempId(entity.getDiscGoodTempId());
            mkStoreTicketGoodsTempService.update(tempEntity);
        }
        return R.ok();
    }

    @RequestMapping("/deleteTemp")
//    @RequiresPermissions("mkstoreticketgoodstemp:deleteTemp")
    @ResponseBody
    public R deleteTemp(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketDiscountGoods) {
        MkStoreTicketGoodsTempEntity entity = mkStoreTicketGoodsTempService.queryGoodsTempByRealId(mkStoreTicketDiscountGoods.getStoreRelaId(), mkStoreTicketDiscountGoods.getGuid());
        if(entity != null){
            Integer[] ids = {entity.getDiscGoodTempId()};
            mkStoreTicketGoodsTempService.deleteBatch(ids);
        }
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkstoreticketgoodstemp:update")
    @ResponseBody
    public R update(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketGoodsTemp) {
        mkStoreTicketGoodsTempService.update(mkStoreTicketGoodsTemp);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("mkstoreticketgoodstemp:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]discGoodTempIds) {
        mkStoreTicketGoodsTempService.deleteBatch(discGoodTempIds);

        return R.ok();
    }

    /**
     * 修改选中商品为无效
     * @param discGoodTempIds
     * @return
     */
    @RequestMapping("/updateIsValidById")
//    @RequiresPermissions("mkstoreticketgoodstemp:updateIsValidById")
    @ResponseBody
    public R updateIsValidById(@RequestBody Integer[]discGoodTempIds) {
        for (int i =0;i< discGoodTempIds.length; i++){
            MkStoreTicketGoodsTempEntity tempEntity = new MkStoreTicketGoodsTempEntity();
            tempEntity.setIsButtonCannel(Dict.isValid.item_1.getItem());//无效
            tempEntity.setDiscGoodTempId(discGoodTempIds[i]);
            mkStoreTicketGoodsTempService.update(tempEntity);
        }
        return R.ok();
    }

    /**
     * 触发优惠券选择商品页面返回按钮时，将返回失效的商品还原为有效
     * @param mkStoreTicketGoodsTemp
     * @return
     */
    @RequestMapping("/resetIsValidByTemp")
//    @RequiresPermissions("mkstoreticketgoodstemp:resetIsValidByTemp")
    @ResponseBody
    public R resetIsValidByTemp(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketGoodsTemp) {
        mkStoreTicketGoodsTempService.resetDiscTicketIsValidByTemp(mkStoreTicketGoodsTemp);
        return R.ok();
    }

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

        List<MkStoreTicketGoodsTempEntity> list = mkStoreTicketGoodsTempService.queryList(params);

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

    @RequestMapping("/queryAllDiscountByStoreId")
    @ResponseBody
    public R queryAllDiscountByStoreId(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        //查询列表数据
        Query query = new Query(params);

        List<MkStoreTicketGoodsTempEntity> list = mkStoreTicketGoodsTempService.queryAllDiscountByStoreId(query);

        int total = mkStoreTicketGoodsTempService.queryTotallDiscountByStoreId(query);

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

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

    /**
     * 触发门店活动选择商品页面分页按钮时保存数据
     * @param mkStoreTicketDiscountGoods
     * @return
     */
    @RequestMapping("/batchSaveStoreTopicGoods")
//    @RequiresPermissions("mkstoreticketgoodstemp:batchSaveStoreTopicGoods")
    @ResponseBody
    public R batchSaveStoreTopicGoods(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketDiscountGoods) {
        mkStoreTicketGoodsTempService.batchSaveStoreTopicGoods(mkStoreTicketDiscountGoods);
        return R.ok();
    }

    /**
     * 取消选中当前门店活动商品，将商品状态修改为无效
     * @param mkStoreTicketDiscountGoods
     * @return
     */
    @RequestMapping("/updateTempByStoreTopicIsValid")
//    @RequiresPermissions("mkstoreticketgoodstemp:updateTempByStoreTopicIsValid")
    @ResponseBody
    public R updateTempByStoreTopicIsValid(@RequestBody MkStoreTicketGoodsTempEntity mkStoreTicketDiscountGoods) {
        MkStoreTicketGoodsTempEntity entity = mkStoreTicketGoodsTempService.queryGoodsTempByRealIdAndTopicId(mkStoreTicketDiscountGoods.getStoreRelaId(),
                mkStoreTicketDiscountGoods.getStoreTopicId());
        if(entity != null){
            MkStoreTicketGoodsTempEntity tempEntity = new MkStoreTicketGoodsTempEntity();
            tempEntity.setDiscGoodTempId(entity.getDiscGoodTempId());
            tempEntity.setIsValid(Dict.isValid.item_1.getItem());
            mkStoreTicketGoodsTempService.update(tempEntity);
        }
        return R.ok();
    }
}
