package com.yijia.customer.controller;
import java.util.List;
import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.DateUtils;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysDeptService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.customer.domain.CustomerGradeSetting;
import com.yijia.customer.service.ICustomerGradeSettingService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 客户优惠等级设置Controller
 *
 * @author yijia
 * @date 2021-01-08
 */
@RestController
@RequestMapping("/customer/setting")
public class CustomerGradeSettingController extends BaseController
{
    @Autowired
    private ICustomerGradeSettingService customerGradeSettingService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户优惠等级设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        startPage();
        List<CustomerGradeSetting> settingList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getDataTable(settingList);
    }

    /**
     * 导出客户优惠等级设置列表
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        List<CustomerGradeSetting> settingList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        ExcelUtil<CustomerGradeSetting> util = new ExcelUtil<CustomerGradeSetting>(CustomerGradeSetting.class);
        return util.exportExcel(settingList, "setting");
    }

    /**
     * 获取客户优惠等级设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerGradeSettingService.selectCustomerGradeSettingById(id));
    }

    /**
     * 新增客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
        customerGradeSetting.setCreateBy(currentUser.getUser().getUserId().toString());
        customerGradeSetting.setCreateTime(DateUtils.getNowDate());
        customerGradeSetting.setDelFlag("0");
        return toAjax(customerGradeSettingService.insertCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 修改客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerGradeSetting.setStationId(currentUser.getUser().getDeptId());
        return toAjax(customerGradeSettingService.updateCustomerGradeSetting(customerGradeSetting));
    }

    /**
     * 删除客户优惠等级设置
     */
    @Log(title = "客户优惠等级设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        int i=0;
        if(ids!=null){
            LoginUser currentUser = SecurityUtils.getLoginUser();
            CustomerGradeSetting customerGradeSetting =new CustomerGradeSetting();
            customerGradeSetting.setUpdateBy(currentUser.getUser().getUserId()+"");
            customerGradeSetting.setUpdateTime(DateUtils.getNowDate());
            customerGradeSetting.setDelFlag("1");
            customerGradeSetting.setId(ids);
            i=customerGradeSettingService.updateCustomerGradeSetting(customerGradeSetting);
        }
        return toAjax(i);
    }

    /**
     * 查询等级名称
     */
    @GetMapping("/gradeList")
    public TableDataInfo gradeList(CustomerGradeSetting customerGradeSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            customerGradeSetting.setStationIdList(list);
            customerGradeSetting.setStationId(null);
        }
        customerGradeSetting.setDelFlag("0");
        List<CustomerGradeSetting> gradeList = customerGradeSettingService.selectCustomerGradeSettingList(customerGradeSetting);
        return getTable(gradeList);
    }
}
