package com.winsun.controller;


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.bean.SalesAlert;
import com.winsun.mapper.SalesAlertMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

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

/**
 * 销售预警 controller
 *
 * @Date: 2021/6/3
 */
@Slf4j
@RestController
@RequestMapping("/salesAlert") //
public class SalesAlertController  extends BaseController {

    @Autowired
    private SalesAlertMapper salesAlertMapper;

    @Permission(menuname = "查询信息", value = "list", method = RequestMethod.POST)
    public Map<String,Object> dataList(@RequestParam(name = "alertType",required = false)String alertType,
                                       @RequestParam(name = "alertArea",required = false)String alertArea,
                                       @RequestParam(name = "name",required = false)String name,
                                       @RequestParam(name = "pageNo", required = false) int pageNo,
                                       @RequestParam(name = "pageSize", required = false) int pageSize
    ){
        Map<String,Object> map = new HashMap<>();
        Wrapper<SalesAlert> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(alertType), "alert_type",alertType);
        wrapper.eq(StringUtils.isNotBlank(alertArea), "alert_area",alertArea);
        wrapper.eq(StringUtils.isNotBlank(name), "name",name);
        List<String> objects = new ArrayList<>();
        objects.add("id");
        wrapper.orderDesc(objects);
        Page<SalesAlert> page = new Page<>(pageNo, pageSize);
        List<SalesAlert> aPackage = salesAlertMapper.selectPage(page, wrapper);
        page.setRecords(aPackage);
        map.put("page", ResponseData.success(page,"查询成功！"));
        return map;
    }

    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public ResponseData<String> addData(@RequestParam(name = "name") String name,
                                        @RequestParam(name = "alertType") String alertType,
                                        @RequestParam(name = "alertArea") String alertArea,
                                        @RequestParam(name = "alertNumber") String alertNumber,
                                        @RequestParam(name = "remarks") String remarks){
        SalesAlert salesAlert = new SalesAlert();
        salesAlert.setName(name);
        salesAlert.setAlertType(alertType);
        salesAlert.setAlertArea(alertArea);
        salesAlert.setAlertNumber(alertNumber);
        salesAlert.setRemarks(remarks);
        try {
            salesAlertMapper.insert(salesAlert);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }

    @RequestMapping(value = "updateData",method = RequestMethod.POST)
    public ResponseData<String> updateData(@RequestParam(name = "id") String id,
                                           @RequestParam(name = "name") String name,
                                           @RequestParam(name = "alertType") String alertType,
                                            @RequestParam(name = "alertArea") String alertArea,
                                            @RequestParam(name = "alertNumber") String alertNumber,
                                            @RequestParam(name = "remarks") String remarks){
        Wrapper<SalesAlert> wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);
        SalesAlert salesAlert = new SalesAlert();
        salesAlert.setName(name);
        salesAlert.setAlertType(alertType);
        salesAlert.setAlertArea(alertArea);
        salesAlert.setAlertNumber(alertNumber);
        salesAlert.setRemarks(remarks);
        try {
            salesAlertMapper.update(salesAlert,wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }

    @RequestMapping(value = "deleteData",method = RequestMethod.POST)
    public ResponseData<String> deleteData(@RequestParam(name = "id") String id){
        if(StringUtils.isBlank(id)){
            return ResponseData.error("");
        };
        Wrapper<SalesAlert> wrapper = new EntityWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id",id);
        try {
            salesAlertMapper.delete(wrapper);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseData.success();
    }
}
