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.SecurityUtils;
import com.yijia.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.CustomerCardSetting;
import com.yijia.customer.service.ICustomerCardSettingService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 客户电子会员卡充值优惠设置Controller
 *
 * @author yijia
 * @date 2021-04-06
 */
@RestController
@RequestMapping("/customer/cardsetting")
public class CustomerCardSettingController extends BaseController
{
    @Autowired
    private ICustomerCardSettingService customerCardSettingService;

    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询客户电子会员卡充值优惠设置列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setStationId(currentUser.getUser().getDeptId());
        startPage();
        List<CustomerCardSetting> settingList = customerCardSettingService.selectCustomerCardSettingList(customerCardSetting);
        return getDataTable(settingList);
    }

    /**
     * 导出客户电子会员卡充值优惠设置列表
     */
    @Log(title = "客户电子会员卡充值优惠设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();

        customerCardSetting.setStationId(currentUser.getUser().getDeptId());
        List<CustomerCardSetting> settingList = customerCardSettingService.selectCustomerCardSettingList(customerCardSetting);
        ExcelUtil<CustomerCardSetting> util = new ExcelUtil<CustomerCardSetting>(CustomerCardSetting.class);
        return util.exportExcel(settingList, "setting");
    }

    /**
     * 获取客户电子会员卡充值优惠设置详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customerCardSettingService.selectCustomerCardSettingById(id));
    }

    /**
     * 新增客户电子会员卡充值优惠设置
     */
    @Log(title = "客户电子会员卡充值优惠设置", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setStationId(currentUser.getUser().getDeptId());
        if(customerCardSetting!=null &&customerCardSetting.getCardEnabledFlag()!=null && !customerCardSetting.getCardEnabledFlag().equals("")){
            SysDept dept = new SysDept();
            dept.setDeptId(currentUser.getUser().getDeptId());
            dept.setIntegralFlag(customerCardSetting.getCardEnabledFlag());
            deptService.updateDept(dept);
        }
        return toAjax(customerCardSettingService.insertCustomerCardSetting(customerCardSetting));
    }

    /**
     * 修改客户电子会员卡充值优惠设置
     */
    @Log(title = "客户电子会员卡充值优惠设置", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setStationId(currentUser.getUser().getDeptId());
        int i =0;
        if(customerCardSetting!=null&&customerCardSetting.getCardEnabledFlag()!=null && !customerCardSetting.getCardEnabledFlag().equals("")){
            if(customerCardSetting.getCardEnabledFlag().equals("1")){
                SysDept dept =new SysDept();
                dept.setDeptId(currentUser.getUser().getDeptId());
                dept.setCardEnabledFlag(customerCardSetting.getCardEnabledFlag());
                i= deptService.updateDept(dept);
                if(i==1) {
                    i = customerCardSettingService.updateCustomerCardSetting(customerCardSetting);
                }
            }else if(customerCardSetting.getCardEnabledFlag().equals("0")){
                SysDept dept =new SysDept();
                dept.setDeptId(currentUser.getUser().getDeptId());
                dept.setCardEnabledFlag(customerCardSetting.getCardEnabledFlag());
                i= deptService.updateDept(dept);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除客户电子会员卡充值优惠设置
     */
    @Log(title = "客户电子会员卡充值优惠设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customerCardSettingService.deleteCustomerCardSettingByIds(ids));
    }

    /**
     * 查询油站电子卡规则设置
     * @param customerCardSetting
     * @return
     */
    @GetMapping(value = "/selectCustomerCardSetting")
    @Transactional
    public AjaxResult selectCustomerCardSetting(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        customerCardSetting.setStationId(currentUser.getUser().getDeptId());
        return AjaxResult.success(customerCardSettingService.selectCustomerCardSetting(customerCardSetting));
    }

    /**
     * 查询电子卡是否启用
     */
    @GetMapping(value = "/selectCardOpen")
    public AjaxResult selectCardOpen(CustomerCardSetting customerCardSetting)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        //查询电子卡是否开启
        SysDept dept  = deptService.selectDeptById(currentUser.getUser().getDeptId());
        CustomerCardSetting cardSetting =new  CustomerCardSetting();
        if(dept!=null &&dept.getCardEnabledFlag()!=null&& dept.getCardEnabledFlag().equals("1")){
            //查询开启的电子卡
            CustomerCardSetting setting =new CustomerCardSetting();
            setting.setStationId(dept.getDeptId());
            cardSetting=  customerCardSettingService.selectCustomerCardSetting(setting);
        }else{
            cardSetting.setCardOilsType("");
        }
        return AjaxResult.success(cardSetting);
    }
}
