package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.Product;
import com.winsun.bean.Regular;
import com.winsun.mapper.ProductMapper;
import com.winsun.mapper.RegularMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

/**
 * 宽带管理
 *
 * @Author: chancy
 * @Date: 2020/2/12 17:26
 */
@Slf4j
@RestController
@RequestMapping("/product") //
public class ProductController extends BaseController {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RegularMapper regularMapper;

    /**
     * 宽带添加
     *
     * @param product 添加对象json字符串
     * @return
     */
    @Permission(menuname = "添加宽带信息", value = "insert", method = RequestMethod.POST)
    public ResponseData<String> insertProduct(@RequestParam("product") String product) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Product pro = JSON.parseObject(product, Product.class);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        pro.setProductCreateTime(timestamp);
        pro.setProductLastUpdateTime(timestamp);
        pro.setProductCreater(user.getName());
        productMapper.insert(pro);
        return ResponseData.success("添加成功！");
    }

    /**
     * 宽带删除
     *
     * @param id 需要删除的id
     * @return
     */
    @Permission(menuname = "删除宽带信息", value = "delete/{id}", method = RequestMethod.POST)
    public ResponseData<String> deleteProduct(@PathVariable("id") String id) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("product_id", id);
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("product_last_update_time", timestamp);
        dataMapping.put("product_state", "1");
        productMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("删除成功！");
    }

    /**
     * 宽带信息编辑
     *
     * @param product 编辑对象json字符串
     * @return
     */
    @Permission(menuname = "修改宽带信息", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updateProduct(@RequestParam("product") String product) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Product pro = JSON.parseObject(product, Product.class);
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("product_id", pro.getProductId());
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("product_last_update_time", timestamp);
        dataMapping.put("product_meal_price", pro.getProductMealPrice());
        dataMapping.put("product_meal", pro.getProductMeal());
        dataMapping.put("product_title", pro.getProductTitle());
        dataMapping.put("product_university", pro.getProductUniversity());
        dataMapping.put("university_id", pro.getUniversityId());
        dataMapping.put("product_price", pro.getProductPrice());
        dataMapping.put("product_region", pro.getProductRegion());
        dataMapping.put("product_rate", pro.getProductRate());
        dataMapping.put("product_state", pro.getProductState());
        dataMapping.put("product_introduction", pro.getProductIntroduction());
        dataMapping.put("product_set_meal_introduction", pro.getProductSetMealIntroduction());
        dataMapping.put("product_rf2", pro.getProductRf2());
        dataMapping.put("product_introduction_pic2", pro.getProductIntroductionPic2());
        productMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }

    /**
     * 寬帶查詢展示
     *
     * @param productUniversity 适用学校
     * @param productRegion     区域
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询宽带信息", value = "list", method = RequestMethod.POST)
    public ResponseData<Map<String,Object>> listProduct(@RequestParam(name = "productUniversity", required = false) String productUniversity, @RequestParam(name = "productRegion", required = false) String productRegion,
                                                   @RequestParam(name = "pageNo") int pageNo, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员", "系统管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper<Product> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(productUniversity), "product_university", productUniversity, SqlLike.DEFAULT);
        wrapper.like(StringUtils.isNotBlank(productRegion), "product_region", productRegion, SqlLike.DEFAULT);
        wrapper.eq("product_state", "0");
        Page<Product> page = new Page<>(pageNo, pageSize);
        List<Product> products = productMapper.selectPage(page, wrapper);

        Wrapper<Regular> regularWrapper = new EntityWrapper<>();
        regularWrapper.eq("is_show",'1');
        List<Regular> regulars = regularMapper.selectList(regularWrapper);
        Map<String,Object> map = new HashMap<>();
        page.setRecords(products);
        map.put("page",page);
        map.put("regulars",regulars);
        return ResponseData.success(map, "查询成功！");
    }

    /**
     * 单宽学校名称
     * @return
     */
    @Permission(menuname = "单宽学校名称", value = "productSchool", method = RequestMethod.POST)
    public ResponseData< List<Map<String, Object>>> productSchool(){
        Wrapper<Product> wrapper = new EntityWrapper();
        wrapper.eq("product_state", "0");
        List<Product> products = productMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>>  list= new ArrayList<>();
        for (Product product:products){
            map.put("id",product.getProductId());
            map.put("productSchool",product.getProductUniversity());
            list.add(map);
        }
        return ResponseData.success(list);
    }
}
