package com.xkrs.controller;

import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.common.tool.TokenUtil;
import com.xkrs.dao.ProductDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.model.qo.ProductQo;
import com.xkrs.service.ProductService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Locale;
import java.util.Map;

import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;

/**
 * @Author: XinYi Song
 * @Date: 2021/12/15 15:42
 */
@RestController
public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private ProductDao productDao;

    /**
     * 添加商品信息
     * @param productQo
     * @param token
     * @return
     */
    @PostMapping("/addingProduct")
    public String addingProduct(@RequestBody ProductQo productQo, @RequestHeader(value="Authorization") String token){
        // 获取区域信息
        Locale locale = LocaleContextHolder.getLocale();
        // 验证token
        String tokenUserName = TokenUtil.getTokenUserName(token);
        SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
        if(sysUserEntity == null){
            return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR,"您还没有注册登录，请先注册登录",locale);
        }
        return productService.addingProduct(productQo,sysUserEntity.getId(),sysUserEntity.getIdentity());
    }

    /**
     * 商家查看商品上下架的信息（0 待审核，1上架，2下架）
     * @param map
     * @param token
     * @return
     */
    @PostMapping("/selectProductShelfType")
    public String selectProductShelfType(@RequestBody Map map, @RequestHeader(value="Authorization") String token){
        Locale locale = LocaleContextHolder.getLocale();
        String productCounty = (String) map.get("productCounty");
        String shelfType = (String) map.get("shelfType");
        // 验证token
        String tokenUserName = TokenUtil.getTokenUserName(token);
        SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
        if(sysUserEntity == null){
            return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR,"您还没有注册登录，请先注册登录",locale);
        }
        return productService.selectProductShelfType(sysUserEntity.getId(),productCounty,shelfType);
    }

    /**
     * 各级查看自己要审核的商品
     * 区县代理商查看联盟商家 0    市级代理商查看区县1 总部查看市级2   总部通过是3   不通过4
     * @param map
     * @param token
     * @return
     */
    @PostMapping("/selectProductReviewType")
    @PreAuthorize("hasAnyAuthority('auth_administor','auth_city','auth_county')")
    public String selectProductReviewType(@RequestBody Map map, @RequestHeader(value="Authorization") String token){
        Locale locale = LocaleContextHolder.getLocale();
        String productCity = (String) map.get("productCity");
        String productCounty = (String) map.get("productCounty");
        String reviewType = (String) map.get("reviewType");
        // 验证token
        String tokenUserName = TokenUtil.getTokenUserName(token);
        SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
        if(sysUserEntity == null){
            return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR,"您还没有注册登录，请先注册登录",locale);
        }
        return productService.selectProductReviewType(sysUserEntity.getId(),productCity,productCounty,reviewType);
    }

    /**
     * 各级进行审核的操作
     * @param map
     * @return
     */
    @PostMapping("/updateReviewType")
    @PreAuthorize("hasAnyAuthority('auth_administor','auth_city','auth_county')")
    public String updateReviewType(@RequestBody Map map,@RequestHeader(value="Authorization") String token){
        Integer productId = (Integer) map.get("productId");
        String reviewType = (String) map.get("reviewType");
        return productService.updateReviewType(productId,reviewType);
    }

    /**
     * 商品下架
     * @param map
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    @PostMapping("/updateOffShelf")
    @PreAuthorize("hasAnyAuthority('auth_affiliate_merchant','auth_city','auth_county')")
    public String updateOffShelf(@RequestBody Map map,@RequestHeader(value="Authorization") String token){
        Locale locale = LocaleContextHolder.getLocale();
        Integer productId = (Integer) map.get("productId");
        productDao.updateShelfTypeById(productId,"2");
        return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"商品下架成功！",locale);
    }
}
